I have a mongoDB collection with multiple users in this format:
_id: 60fd0e98ead1734a1cf735c7
username: "some username"
__v: 0
I can create new users with this database using another POST method. However, I couldn't get it to update. The browser will return the expected result with res.send(result). However, when I check, mongoDB database is never changed. Is there an extra step that mongoDB requires? If the res.send(result) is correct in the browser, where is it reside if not in mongoDB? Here's the code:
const Schema = mongoose.Schema
const usernameSchema = new Schema({username: String,})
const username = mongoose.model("username", usernameSchema)
app.post('/api/users/:_id/exercises', async (req, res) => {
let user_id = req.body._id
let query = { _id: user_id }
let new_username = req.body.username
username.findOne(query, function (err, result) {
if (err) {
console.log(err);
res.sendStatus(500);
return
}
if (result) {
result.username = new_username;
res.send(result)
}
})
})