简体   繁体   English

猫鼬gridfs-stream如何填充fs.file集合

[英]Mongoose gridfs-stream how to populate fs.file collection

I'm using gridfs-stream module in nodejs for storing images. 我在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
});

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. 我知道我的数据库中有fs.files和fs.chunks。 but should I create the schema for them so that i could reference my model? 但是我应该为他们创建架构以便我可以引用我的模型吗?

user.photo is of type Schema.Types.ObjectId. user.photo的类型为Schema.Types.ObjectId。 Therefore, user.photo.filename will not exist. 因此,user.photo.filename将不存在。

You need to have the image files uploaded to gridfs and get the fileId given to the image in the fs.files collection. 您需要将图像文件上传到gridfs,并在fs.files集合中获取为图像指定的fileId。 You can then set user.photo to be the fileId of the file in fs.files. 然后可以将user.photo设置为fs.files中文件的fileId。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM