I’m doing the API and microservices challenge #3, the exercise tracker.
I need to return an object with an array that’s in MongoDB and add a key-value pair “count” to the object.
This is my code
app.get("/api/exercise/log", (req,res) => {
User.findById(req.query.userId)
.select({__v: 0, _id: 0, username: 0})
.exec((err, docs) => {
if (!err) {
docs.count = docs.log.length;
res.json(docs)
}
})
})
For some reason, docs.count is not being added, the value it’s being assigned to exists, it’s 6, but I also tried button the value myself and tried using the notation instead of the dot notation, none of those worked.
This is the structure of “docs”
var docs = {
log: [
{
duration: 10,
date: "Sun Jan 03 2021",
description: "desc"
}
],
count: 1 -- this is where count needs to be added, but I can't add it for some reason
}