Search code examples
node.jsmongodbmongoose

MongoDB returns undefined while using find()


When I try to get the data using the find() method, it returns undefined even it has data within it.

let data = await submissionSchema.find({ reviewed: false }).exec();
console.log(data[0].password, data[0]);

it returns the followings

undefined {
  _id: new ObjectId("635fdf4754a0c724794209b3"),
  timestamp: '2022/10/31 22:00:00',
  content: 'TESTING 1 2 3',
  reviewed: false,
  referenceID: '202300763061',
  password: 'asgsag'
}

It is quite weird as I can get the value when I log other properties of that data such as content, timestamp etc. All I cannot get is password one, any clues on it?


Solution

  • Okay I have figured out the reason of it is returning undefined. So I am here to answer my question.

    I haven't included the password properties in my schema declaration.

    const submissionSchema = new mongoose.Schema({
      // other properties...
      password: String
    });
    

    By including the required properties, I would be able to get the data with the find() method.