Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving image with mongoose

I know there are quite a few threads on this topic already, but unfortunately I didn't find my answer until now. I use angular.js with the example code from http://angular-js.in/image-upload/ to get the image from the client. This part works

Now to the node/mongodb part, here's my backend model:

var userSchema = new mongoose.Schema({
    avatar: { data: Buffer, contentType: String },
    // ...
});

module.exports = mongoose.model('User', userSchema);

and my node code:

exports.createAvatar = function (req, res) {
  var avatar = {
    data: req.body.data.image, // see below
    contentType: 'image/png'
  }

  models.DUser
    .findById(index.findUserId(req))
    .exec(function (err, user) {
      user.avatar = avatar;
      // ...
      user.save(function (err, user) {/* ... */ });

and my angularCtrl:

var foo = {
  image: image,
  imageName: image.name,
};

$http.post('/api/users', {data: foo })
    .success(function (result) { /*...*/ });

Beside from req.body.data.image I tried many different variations like req.body.data.image.dataURL, req.body.data.image.dataURL.data, but nothing worked so far. My Logging of req.body.data.image shows:

{ file: 
   { webkitRelativePath: '',
     lastModified: 1411073963000,
     lastModifiedDate: '2014-09-18T20:59:23.000Z',
     name: '3770316278.png',
     type: 'image/png',
     size: 32493 },
  url: 'blob:http%3A//127.0.0.1%3A3000/cb20debc-8a3a-468f-ab5c-39299f7ec52b',
  dataURL: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACHCAYAAAC.....

How can I save the image to the database?

edit

I tried to save everything after base64, from req.body.data.image.dataURL into the avatar like this:

var split = req.body.data.image.dataURL.split('base64,');
var type = split[0];
var data = split[1];

var avatar = {
    data: type,
    contentType:'image/png'
};

the save message is still:

user.avatar = avatar
user.save(function (err, user) {}

But I still get the error

TypeError: Cannot set property 'avatar' of null


since my question changed a bit, I'm marking this one as solved, the new question is here: Displaying Images in Angular.js from MongoDB

like image 807
Markus Avatar asked Dec 08 '14 07:12

Markus


People also ask

Can Mongoose store images?

So for storing an image in MongoDB, we need to create a schema with mongoose. For that create the file `model. js` file and define the schema. The important point here is that our data type for the image is a Buffer which allows us to store our image as data in the form of arrays.

Can we save images in MongoDB?

The short answer is: Yes, you can store (small) images if you encode them correctly with base64 , see stackoverflow.com/questions/11442356 I also found menge.io/2015/03/24/storing-small-images-in-mongodb a very good starting point.

Can MongoDB store images and videos?

GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB.


1 Answers

You know, definitely it's not the best way to save image data on mongo. Instead you can save your image on any directory on your server and save only url of your file, like..

mongoose-model.js

avatar : {
    type : String
}

and on your main script you should get

let avatar_url = '/path/to/uploads/' + req.body.data.image.file.name

something like this

like image 167
Hakob Tumanyan Avatar answered Sep 25 '22 19:09

Hakob Tumanyan