What i am trying to create, find a comment based on commentId also finding that particular comment which should not be postedBy currently loggedin user
I am trying to create commentVote functionality, loggedIn user cant vote his comment or post.
getting null as a result
my voteComment Controller Method –
exports.voteComment = (req, res) => {
const { commentId } = req.params;
if (!ObjectId.isValid(commentId)) {
return res.status(400).send({
error: "Comment ID not valid",
});
}
console.log(typeof req.auth._id);
Comment.findOneAndUpdate(
{ commentId, "comment.postedBy": { $ne: req.auth._id } },
{ $push: { votes: req.auth._id } },
{ new: true }
// { omitUndefined: true }
// { returnNewDocument: true }
).exec((err, result) => {
if (err) {
return res.status(400).json({
error: "Could not vote on that comment",
});
}
console.log(result);
res.json(result);
// if (result.nModified) {
// console.log("test");
// }
});
My Comment Model-
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const commentSchema = new mongoose.Schema(
{
content: {
type: String,
required: true,
trim: true,
max: 200,
},
postedBy: {
type: ObjectId,
ref: "User",
required: true,
},
feature: {
type: ObjectId,
ref: "Feature",
required: true,
},
votes: [{ type: ObjectId, ref: "User" }],
replies: [
{
content: {
type: String,
required: true,
trim: true,
max: 200,
},
postedBy: {
type: ObjectId,
ref: "User",
},
created: {
type: Date,
default: Date.now,
},
votes: [
{
postedBy: {
type: ObjectId,
ref: "User",
},
created: {
type: Date,
default: Date.now,
},
},
],
},
],
},
{ timestamps: true }
);
module.exports = mongoose.model("Comment", commentSchema);
I also want to create error handling so that if Comment isn’t updated so i can modify it by checking if(comment.nModified)
but i dont know how to use .nModified and .ok on response from mongoose