1

This is what I have so far. This is my AnswerSchema with a comments array nested within that I am trying to update.

const AnswerSchema = new Schema({
      user: {
        type: Schema.Types.ObjectId,
        ref: 'user',
      },
      question: {
        type: Schema.Types.ObjectId,
        ref: 'question',
      },
      text: {
        type: String,
        required: true,
      },
      name: {
        type: String,
      },
      avatar: {
        type: String,
      },
      views: {
        type: Number,
      },
      date: {
        type: Date,
        default: Date.now,
      },
      answerLikes: [
        {
          user: {
            type: Schema.Types.ObjectId,
            ref: 'user',
          },
        },
      ],
      comments: [
        {
          user: {
            type: Schema.Types.ObjectId,
            ref: 'user',
          },
          text: {
            type: String,
            required: true,
          },
          name: {
            type: String,
          },
          avatar: {
            type: String,
          },
          commentLikes: [
            {
              user: {
                type: Schema.Types.ObjectId,
                ref: 'user',
              },
            },
          ],
          date: {
            type: Date,
            default: Date.now,
          },
        },
      ],
    })

and here is my update route that I am trying to use to update the comments array text field

try {
    const updatedAnswer = await Answer.findOneAndUpdate(
      { _id: req.params.answer_id },
      {
        $set: { 'comments.$[comment].text': formattedAnswer },
      },
      {
        arrayFilters: [{'comment._id': req.params.comment_id }],
      },
      { new: true }
    )
    res.json(updatedAnswer)

I keep getting the error 'Callback must be a function, got [object Object]' and cant figure out a fix. Any ideas? Thanks!

2 Answers 2

2

The problem in your code is that you are passing 4 parameters to the findOneAndUpdate function. The 4th argument is a callback which accepts a function:

(err /* an error if occurred */, doc /* the updated document */) => {}

In order to solve that you need to combine your last 2 arguments into one object like:

{
    arrayFilters: [{'comment._id': req.params.comment_id }],
    new: true
}

Final query:

const updatedAnswer = await Answer.findOneAndUpdate(
    { _id: req.params.answer_id },
    {
        $set: { 'comments.$[comment].text': formattedAnswer },
    },
    {
        arrayFilters: [{'comment._id': req.params.comment_id }],
        new: true
    }
)
Sign up to request clarification or add additional context in comments.

Comments

0

The 4th argument in findOneAndUpdate function takes in a callback function that was where your error was.

Try this

try{

  const updatedAnswer = await Answer.findOneAndUpdate(
   { _id: req.params.answer_id },
   {
      $set: { 'comments.$[comment].text': formattedAnswer },
   },
   {
     arrayFilters: [{'comment._id': req.params.comment_id }],
     new: true
   }
   );

   res.json(updatedAnswer);

}catch(err){
  //console.log(err)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.