node.jsmongoosegridfsgridfs-stream

Mongoose returns undefined on a field with existing data


So I'm trying to associate GridFS collection with schema containing some of the data in it and then populating it with another model.

GridFS Schema:

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

Code used:

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

The first console.log returns what I expected:

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

But the second one returns undefined, even though the example above shows there's existing data in the filename field.

I need filename field in order to visualize images when populating.


Solution

  • files is an array so you need to call it like

    files[0].filename
    

    if your required filename is at first place, else you can use index according to requirement