0

I am pulling objects from MongoDb using mongoose:

database.js

  async fetchReplies(language, rating, keywords) {
    return await Reply.find({ $and: [
      { languageCode: language },
      { stars: rating },
      (keywords.length == 0) ? { keywords: keywords } : { keywords: { $in: keywords } }
    ]});
  }

api.js

  async findReply(reviewLanguage, reviewRating, reviewKeywords) {
    return await Database.fetchReplies(reviewLanguage, reviewRating, reviewKeywords);
  }

When trying to get the data and print it:

  // Working ->
  const reply = await this.findReply(reviewLanguage, reviewRating, []);
  console.log(`Found a reply to this review: ${reply}`);

  // Prints 'undefined' ->
  console.log(`Reply comments: ${reply.comments}`);

The response:

Found a reply to this review: [object Object]
Reply comments: undefined

And sometimes it prints it like that:

Found a reply to this review: {
  _id: new ObjectId("62f6dfb13eb6410001621b99"),
  languageCode: 'en',
  stars: [ 5, 4 ],
  keywords: [],
  comments: [
    'Thank you. We are happy to know you like it. Please do check out our other games.',
    'Thanks for your review! Please recommend our app to your friends, and don’t hesitate to shoot us a note at if you have any questions.',
    'Thanks so much for the awesome review. Please tell your friends about the game and what they are missing out on :)',
    'Thank you for your feedback! Feel free to let us know what you’d like to see in the game!',
    'We pleased that you enjoyed the game. If there is anything we can do for you, just let us know at! Have a wonderful day!'
  ],
  createdAt: 2022-08-12T23:18:09.866Z,
  updatedAt: 2022-09-13T12:09:49.889Z,
  __v: 0
}

What am I doing wrong?

ytpm
  • 4,962
  • 6
  • 56
  • 113
  • Try stringifying: `${JSON.stringify(reply)}` that might not be the object structure you expect – Joe Feb 01 '23 at 01:32

0 Answers0