【问题标题】:Mongoose gridfs-stream how to populate fs.file collectionMongoose gridfs-stream 如何填充 fs.file 集合
【发布时间】:2014-07-17 11:30:41
【问题描述】:

我在 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。但是我应该为他们创建架构以便我可以引用我的模型吗?

【问题讨论】:

    标签: javascript node.js mongoose gridfs-stream


    【解决方案1】:

    user.photo 的类型为 Schema.Types.ObjectId。因此,user.photo.filename 将不存在。

    您需要将图像文件上传到 gridfs 并获取 fs.files 集合中图像的 fileId。然后您可以将 user.photo 设置为 fs.files 中文件的 fileId。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 2019-01-14
      • 2019-05-26
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2019-08-07
      • 2016-02-20
      相关资源
      最近更新 更多