Create/Update Comments Test Error When Using ObjectId(commentId)

When I wrap the commentId with the ObjectId(), test throws the error below:
console.error src/dao/commentsDAO.js:84
Unable to update comment: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

console.error src/dao/commentsDAO.js:84
Unable to update comment: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Also, the test is returning a different document and not the one I created in the test case for the create comments test.

The fun part is the status validation is successful.

Is there a way around this?

I get this error too.

I think the solution is NOT to wrap commentId with the ObjectId().

Still, when i try to add a new comment, the test
expect(movieComments[0]._id).toEqual(postCommentResult.insertedId)
does fail.

In fact, i cannot find the comment with the insertedId in the comments collection (searching with Compass). I am not sure if the comment is inserted or not, because insertedCount is equal to 1.

Thank you for your help,
Frédéric

Getting the same error.
However, when I add a comment using the UI, the comment is saved and updated successfully.
I am not certain this assertion is correct given the order in which the saved comments are returned.

expect(movieComments[0]._id).toEqual(postCommentResult.insertedId)

Are the comments supposed to be sorted by date in order of most recent?

can confirm that the comments were to be sorted by date in order of most recent. seemed to have overlooked that bit while reading the question for “Get comments”. Once I added the sort, I was able to validate the ticket.

1 Like

Getting the same error.

41

**expect(movieComments[0]._id).toEqual(postCommentResult.insertedId)

Apply the same solution, sort the comments as indicated in the lab requirements.

hi, sorry, i don’t understand you, this is my code:

const pipeline = [
    {
      $match: {
       _id: ObjectId(id)
      }
    },
    {
      $lookup :{
          from: 'comments',
              let: { id: "$_id" },
                 pipeline: [
                    { $match:
                       { $expr:
                          { $eq: [ "$movie_id",  "$$id" ] },
                       }
                    },
                 ],
        as: 'comments'
      }
    },
    {
      $sort : {
        date : 1
      }
    },
  ]

You need to sort the comments in the lookup pipeline stage.

Kanika

And most recent first.

1 Like

you’re currently sorting the comments in order of oldest first. adjust the sort directive to sort comments by newest first instead

ok!!, thanks. -1 to specify descending order.

                    {
                      $sort : {
                        date : -1
                      }
                    },