I’m having trouble inserting a purchased course by its course ID into the user’s document in the MongoDB collection using Express.js and Mongoose. The insertion does not seem to be happening correctly, and the purchased course isn’t appearing in the user’s Table.
Also i Have provided appropriated headers.
<code>router.post('/courses/:courseId', userMiddleware, async(req, res) => {
// Implement course purchase logic
const courseId = req.params.courseId;
const username = req.headers.username;
await User.updateOne({
username: username
}, {
"$push": {
purchasedCourses: courseId
}
})
res.json({
message: "Purchase complete!"
})
});
</code>
<code>router.post('/courses/:courseId', userMiddleware, async(req, res) => {
// Implement course purchase logic
const courseId = req.params.courseId;
const username = req.headers.username;
await User.updateOne({
username: username
}, {
"$push": {
purchasedCourses: courseId
}
})
res.json({
message: "Purchase complete!"
})
});
</code>
router.post('/courses/:courseId', userMiddleware, async(req, res) => {
// Implement course purchase logic
const courseId = req.params.courseId;
const username = req.headers.username;
await User.updateOne({
username: username
}, {
"$push": {
purchasedCourses: courseId
}
})
res.json({
message: "Purchase complete!"
})
});
What is Error in these post request and help me to fixed the problem.
These is output of Postman Api
–
and The Following is MongoDb UI -
I have tried try catch block to find error but these strategy is also not work.
1