Create, Update, Delete Comments

Hi everyone,

I am encountering some issues that I do not understand when trying to implement the Create/Update Comments ticket and the Delete Comments ticket.

WIth the Create/Update Comments ticket if I assign the movieId parameter to the movie_id field (along with assigning name and email fields to the user object. etc…), I am only able to pass one test. For the sake of not leaving out anything that might cause confusion, I am posting the entire output of npm test -t create-update-comments:

 FAIL  test/create-update-comments.test.js
  Create/Update Comments
    ✕ Can post a comment (264ms)
    ✕ Can update a comment (112ms)
    ✓ Can only update comment if user posted comment (111ms)

  ● Create/Update Comments › Can post a comment

    expect(received).toEqual(expected)

    Expected value to equal:
      "5c709510bbcbb52ac114600b"
    Received:
      "5b7223c420a3277d2194614a"

    Difference:

    - Expected
    + Received

    - "5c709510bbcbb52ac114600b"
    + "5b7223c420a3277d2194614a"

      55 |     const martianComments = (await MoviesDAO.getMovieByID(movieId)).comments
      56 |
    > 57 |     expect(martianComments[0]._id).toEqual(postCommentResult.insertedId)
         |                                    ^
      58 |     expect(martianComments[0].text).toEqual(comment.text)
      59 |
      60 |     comment.id = postCommentResult.insertedId

      at toEqual (test/create-update-comments.test.js:57:36)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
      at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
      at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

  ● Create/Update Comments › Can update a comment

    expect(received).toBe(expected) // Object.is equality

    Expected: 1
    Received: 0

      69 |     )
      70 |
    > 71 |     expect(updateCommentResult.modifiedCount).toBe(1)
         |                                               ^
      72 |
      73 |     const martianComments = (await MoviesDAO.getMovieByID(movieId)).comments
      74 |

      at toBe (test/create-update-comments.test.js:71:47)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
      at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
      at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

Test Suites: 1 failed, 1 total
Tests:       2 failed, 1 passed, 3 total
Snapshots:   0 total
Time:        2.329s, estimated 3s
Ran all test suites matching /create-update-comments/i.
Teardown Mongo Connection

…strange thing is if I is that if I wrap the movieId with ObjectId I get an entirely different result (which still results in failure of 2 of the tests):

Create/Update Comments
    ✕ Can post a comment (26ms)
    ✕ Can update a comment (120ms)
    ✓ Can only update comment if user posted comment (120ms)

  ● Create/Update Comments › Can post a comment

    expect(received).toBe(expected) // Object.is equality

    Expected: 1
    Received: undefined

    Difference:

      Comparing two different types of values. Expected number but received undefined.

      50 |     )
      51 |
    > 52 |     expect(postCommentResult.insertedCount).toBe(1)
         |                                             ^
      53 |     expect(postCommentResult.insertedId).not.toBe(null)

So with regards to my first example, it appears that the test is indeed “creating” a comment, but it’s just not the right comment with right associated ID.

The other issue that I’m facing is with comments.deleteOne method. When I run npm test -t delete-comments with _id: ObjectId(commentId) set to its default (ie., as provided in the handout), I am receiving an error caused by the ObjectId (even though BSON is imported at the top of the file):

 Delete Comments
    ✕ Can post a comment (265ms)
    ✕ Cannot delete a comment if email does not match (3ms)
    ✕ Can delete a comment if email matches (3ms)

  ● Delete Comments › Can post a comment

    expect(received).toEqual(expected)

    Expected value to equal:
      "5c70981246f2d12bd61f7c66"
    Received:
      "5a9427648b0beebeb6957e1b"

    Difference:

    - Expected
    + Received

    - "5c70981246f2d12bd61f7c66"
    + "5a9427648b0beebeb6957e1b"

      41 |     const kingKongComments = (await MoviesDAO.getMovieByID(movieId)).comments

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

If I take away the ObjectId() wrapper from the deleteOne function, I get the most of the same error(s) as above, minus the bit about having to be a single String (Error: Argument passed in must be a single String of 12 bytes). There is one test that passes, however:

 Delete Comments
    ✕ Can post a comment (261ms)
    ✓ Cannot delete a comment if email does not match (117ms)
    ✕ Can delete a comment if email matches (115ms)

Has anyone else encountered these issues? If so, is there a hint as to how to fix these problems?

Thanks in advance to anyone who can help.

1 Like

Could you please share how the commentDoc looks like at the very begin of
static async addComment(movieId, user, comment, date) ?
In my case the problem occurred due to the value I set for movie_id.

Hi @Alex_68427,

I hope that it’s ok to post this here…being that it’s not the entire code block.

This is the value that I have for movie_id:

movieId

I also tried:

ObjectID(movieId)

Thanks

try new ObjectID(movieId)

This is what is returned when I append new to ObjectId:

expect(received).toBe(expected) // Object.is equality

Expected: 1
Received: undefined

Difference:

  Comparing two different types of values. Expected number but received undefined.

Well, it looks like your addComment does not work.
What you can try is modifying create-update-comments.test.js file by adding logging of addComment call result:
console.log(postCommentResult)

right before
line 56: expect(postCommentResult.insertedCount).toBe(1)

Thanks @Alex_68427 ,

For me, `expect(postCommentResult.insertedCount).toBe(1) is on line 40. I placed the log on line 39, but I’m not seeing any output when I re-run the test.

I was wondering…being that the user parameter is an object… Are you using dot notation to assign values to the name and ``emailfields in thecommentDoc` object?

Thanks-

I do use properties name and email of object user to set values for keys name and email in the commentDoc document.

@juliettet, I suspect that your getMovie does not return the comments sorted descending. Since C,U,D Comments uses getMovie if the movies are not in the correct order all tests will fail.

2 Likes

I think the problem is: we need an id for user that is identified by email
Am I right?
I do not know how to do this; We have to find in db that user and I think for that we need to aggregate a search for that user
Am I right?
Thanks for hepl
mmt

@steevej-1495,

I do have the comments sorted (within the $lookup pipeline) in descending order. I ran my aggregation pipeline in Compass and I can see that the comments are sorted from newest to oldest.

Thanks :grinning:

Does C,U,D Comments work now or still have an issue?

I am still unable to C,U,D comments :frowning:

Looking at this:

@param {Object} user - An object containing the user's name and email.

Being that the fields name and email are contained within the user object, wouldn’t that mean that in order to access those values I’d have to use dot notation (when assigning values to the name and email fields within commentDoc) to get the object’s properties? This is what I’m doing. As for the other fields, I’ve just assigned the associated parameters to the created fields as described in the instructions.

May be this is the problem. The field for the comment is called comment it is called text. If you look at the existing movies. So the commentDoc needs to have ‘text : comment’.

The function document says:

* Inserts a comment into the comments collection, with the following fields:

- “name”, the name of the user posting the comment
- “email”, the email of the user posting the comment
_- “movie_id”, the id of the movie pertaining to the comment
- “text”, the text of the comment
- “date”, the date when the comment was posted

The tiket is
TODO Ticket: Create/Update Comments
My test said:
Difference:

- Expected
+ Received

- "5c71bc56685ef2224cd5f941"
+ "5b72236520a3277c015b3b73"

  55 |     const martianComments = (await MoviesDAO.getMovieByID(movieId)).comments
  56 | 
> 57 |     expect(martianComments[0]._id).toEqual(postCommentResult.insertedId)
     |                                    ^
  58 |     expect(martianComments[0].text).toEqual(comment.text)
  59 | 
  60 |     comment.id = postCommentResult.insertedId

This makes me think it’s an update not a new comment insert

mmt

I hope that you’re not getting too annoyed with me at the moment (:smirk:), but I already have the comment parameter assigned to the text field.

Hi @mmt,

Well, at least we know that we’re both getting something to happen… Did you also include a sort stage within your $lookup pipeline within the getMovieById method in movieDAO.js?

Were you able to implement the addUser method successfully via the User Management ticket? …just curious… Thanks-

I’m not sure, but I think that there may be an issue with the parameters that I’m assigning to the name and email fields, but I can’t seem to figure it out. I’ve tried every possible configuration that I can think of.

…or…maybe, just maybe (guessing in the wind again), I can’t get the C,U,D tests to pass due to the fact that I was never able to figure out how to implement the addUser method correctly…

Do you still have the problem? I only managed to pass this ticket when I got the comments sorted out by date, and you did say you get it right? Another IMPORTANT thing is that even though the test fails, the status page gives the correct hash for the ticket! It took me a long time to find out how to sort the comments and then another couple of hours to ignore the test and check the Status page. Hope that this helps someone!

1 Like