我使用nodejs中的gridfs-stream模块来存储图像。
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
});如何填充用户照片,以便访问文件信息?
我知道我的数据库里有fs.files和fs.chunks。但是,我是否应该为它们创建模式,以便可以引用我的模型?
发布于 2014-12-12 00:30:56
user.photo的类型为Schema.Types.ObjectId。因此,user.photo.filename将不存在。
您需要将图像文件上传到gridfs,并在fs.files集合中为图像提供fileId。然后,您可以在fs.files中将user.photo设置为文件的fileId。
https://stackoverflow.com/questions/23908281
复制相似问题