【问题标题】:Mongoose returns undefined on a field with existing dataMongoose 在具有现有数据的字段上返回未定义
【发布时间】:2019-10-13 23:29:56
【问题描述】:

所以我试图将 GridFS 集合与包含其中一些数据的架构相关联,然后用另一个模型填充它。

GridFS 架构:

const gfsSchema = mongoose.Schema({
    filename: String
  }, {strict: false});
const GridFs = conn.model('GridFs', gfsSchema, 'posters.files');

使用的代码:

GridFs.find({}, (err, files) => {
  console.log(files);
  console.log(files.filename);
});

第一个console.log 返回我所期望的:

[ { _id: 5da3a37a6587f015783637d0,
    length: 3917314,
    chunkSize: 261120,
    uploadDate: 2019-10-13T22:21:54.389Z,
    filename: 'ed49b55f58b1d5ea06ba95f18852e2a3.png',
    md5: '9b52103d5c6f671023290d87b106c9cf',
    contentType: 'image/png' } ]

但第二个返回undefined,尽管上面的示例显示filename 字段中有现有数据。

我需要filename 字段以便在填充时可视化图像。

【问题讨论】:

  • files 是一个数组,所以它应该是files[0].filename

标签: node.js mongoose gridfs gridfs-stream


【解决方案1】:

files 是一个数组,所以你需要像

一样调用它
files[0].filename

如果你需要的文件名在第一位,否则你可以根据需要使用索引

【讨论】:

  • 确实有效。非常感谢 @Savan Padaliya, Cuong Le Ngoc 的帮助。
猜你喜欢
  • 2014-04-04
  • 2017-02-02
  • 1970-01-01
  • 2021-02-05
  • 2020-01-18
  • 2020-08-24
  • 2020-07-11
  • 2020-09-15
  • 2018-06-17
相关资源
最近更新 更多