Im mapping over an array of objects and adding a value for "latestMessage"
The latest() function searches for the message and returns and object. But the reult of the map does not contain the new property. If I remove the latest function and just put a string though it works.
function latest(userId) {
Chat.findOne({ $or: [{ to: userId }, { from: userId }] }, {}, { sort: { createdAt: -1 } }, function (err, msg) {
return msg
})
}
Does not work:
let array = users.map(v => ({ ...v._doc, latestMessage: latest(v._id) }))
Works:
let array = users.map(v => ({ ...v._doc, latestMessage: {test: 'test'} }))