我在 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。但是我应该为他们创建模式以便我可以引用我的模型吗?
最佳答案
user.photo 是 Schema.Types.ObjectId 类型。因此,user.photo.filename 将不存在。
您需要将图像文件上传到 gridfs 并获取 fs.files 集合中为图像指定的 fileId。然后,您可以将 user.photo 设置为 fs.files 中文件的 fileId。
关于javascript - Mongoose gridfs-stream 如何填充 fs.file 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23908281/