1

In Node JS I have an endpoint where I am trying to get data from two different mongo collections and when trying to piece the data together I am not able to add another property to the JSON object.

const getLessonWithTopics = async (req, res) => {
    const lessonId = req.params.id;

    // Get the lesson
    Lesson.findOne({_id: lessonId}).exec((err, data) => {
        let lesson = data;
        Topic.find().where('_id').in(data.topics).exec((err, topics) => {
            if(err) res.status(500).send("Error something went wrong");
            lesson.associatedTopics = topics;
            console.log(lesson);
            res.json(lesson)
        })
    })
}

When logging lesson to console it does not have the associatedTopics property even though through searching online I have found multiple instances where some is saying this is how you would add this property. (Ex. Add new attribute (element) to JSON object using JavaScript )

I have tried using var as well, to see if that would change something (maybe make it mutable) it did not.

** When logging the topics object to console it does log the data that I expected so the variable 'topics' is not the issue **

I'm sure that it is something simple that I am missing and hoping someone with a large brain can help figure this out for me.

Any help would be appreciated, Thank you!

Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17
Will 40
  • 186
  • 5
  • 3
    This question lacks context, which is very importante. Assuming that you're using mongoose, keep in mind that all these returned results/objects are not JSON objects, they're instances of a custom prototype (class), that make them behave differently in comparison to plain javascript objects. In order to you convert these resulting instances to pure javascript objects, you need to call ```.toObject()```. Try search in mongoose docs how you use [lean](https://mongoosejs.com/docs/tutorials/lean.html) – Jone Polvora Aug 17 '22 at 01:08
  • Yes sorry I am using mongoose, I did not know that thank you. Let me try and see if that works – Will 40 Aug 17 '22 at 01:11
  • 2
    No sure with plain mongodb driver. When using mongoose you can convert a document to a plain object like this: `let lesson = data.toObject();` – Peter Thoeny Aug 17 '22 at 01:14
  • Thank you Jone, that worked. I will make sure I include libraries I am using next time as well. I'm a total newbie if you can't tell – Will 40 Aug 17 '22 at 01:14
  • you're welcome @Will40, I'm very glad that I could help you solving this issue. – Jone Polvora Aug 17 '22 at 01:16

1 Answers1

1

Can you try once with the following code?

const getLessonWithTopics = async (req, res) => {

const lessonId = req.params.id;

// Get the lesson
try {
    const lession = await Lession.findOne({ _id: lessonId }).lean()
    const topics = await Topic.find({ _id: { $in: lession.topics } }).lean()
    lession.associatedTopics = topics
    res.json(lesson);
} catch (err) {
    console.log(err) 
    res.status(500)
}
Drashti Kheni
  • 1,065
  • 9
  • 23
  • Yes i learned that the lean function does work to return an object on findOne under mongoose. Thank you for all the help – Will 40 Aug 19 '22 at 20:13