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!