Reputation: 66
I'm using gridfs-stream module in nodejs for storing images.
var db = require('config/db');
var Schema = require('mongoose').Schema;
var schema = new Schema({
name: String,
photo: Schema.Types.ObjectId // store file ObjectId
});
var model = db.model('User', schema);
model.findById('123...', function(err, user) {
console.log(user.photo) // only returning file ObjectId
console.log(user.photo.filename) // error
});
how can i populate the user photo so that i could access the file information?
i know there're fs.files and fs.chunks in my database. but should I create the schema for them so that i could reference my model?
Upvotes: 2
Views: 1051
Reputation: 513
user.photo is of type Schema.Types.ObjectId. Therefore, user.photo.filename will not exist.
You need to have the image files uploaded to gridfs and get the fileId given to the image in the fs.files collection. You can then set user.photo to be the fileId of the file in fs.files.
Upvotes: 1