0

For example, I have a Scheme like this:

var User_schema = new Schema({
  name: string,
  image:{
   avartar_image: String,
   thumbnai_image: String,
   story_image: String
  }
});

Is it possible to create one field have many value like that in nodejs and mongoose? And one more, how can I store the image into mongodb and can push back to client using mongoose?, I store image on mongodb with Buffer, like this :

var Imgschema = new Schema({
    img: Buffer;
});

And if send it to the user when user request a link on server, I do like this:

app.get('/', function (req, res, next) {
        Imgschema.findById('5cdnjwjadhad_ad14', function (err, object) {
          res.contentType('image/png');
          res.send(object.image, {imageToClient: object.image});
        });
      });

It will send the image to the user with full screen, but I want the image display inside the tag with view engine, something like this:

<img src="{{imageToClient}}">

Any one help please, Thanks.

Đức Huỳnh
  • 394
  • 3
  • 17

1 Answers1

0
  1. Of course you can. There are two ways to store a nested object with mongoose. You can use a subdocument, or just nest the object like you did. The difference between two methods is only a middleware and autogenerated _id field. Here is a good answer: Mongoose subdocuments vs nested schema

  2. Storing images as a buffer in a document is not a good idea, but if you want, you can use base64 decoding to show image in browser.

<img src="{{ 'data:image/png;base64,' + imageBuffer.toString('base64') }}" />
SeongMin Park
  • 75
  • 1
  • 7