How to save base64 directly to mongoose in express js
<p>I want to save an image not in my server but in my database.</p>

<p>this is my model</p>

<pre><code>var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var categorySchema = new Schema({
    img: { data: Buffer, contentType: String },
});

module.exports = mongoose.model('Category', categorySchema);
</code></pre>

<p>now in my router I'm getting this base64 image with a long string. </p>

<pre><code>{ '$ngfDataUrl': 'data:image/png;base64,long-String'}
</code></pre>

<p>I want to know how do I save this information in my mongoose db database so far i have this.</p>

<pre><code>router.post('/add', function (req, res) {

var category = new Category();
category.img.data = req.body.category.img;

category.img.contentType = 'image/png';

category.save(function (err) {
    if (err) throw new Error(err);
    res.sendStatus(200)
});
</code></pre>

<p>but obviously that is not working i get this error.</p>

<blockquote>
  <p>Error: ValidationError: CastError: Cast to Buffer failed for value "{
  '$ngfDataUrl': 'data:image/png;base64, long-String'}</p>
</blockquote>

<p>Thanks in advance I'm in saving files.</p>