Create/Update Comments

I’ve passed all the get comment tests(sort by date is working).
I got the result ok when adding a comment, but the added comment still cannot be retrieved.

Any help appreciated, Thanks.

console.log test/create-update-comments.test.js:58
        CommandResult {
          result:
           { n: 1,
             opTime: { ts: [Timestamp], t: 1 },
             electionId: 7fffffff0000000000000001,
             ok: 1,

console.log test/create-update-comments.test.js:56
    postCommentResult.insertedId = 5c45ed25c4f15027e060f731

  console.log test/create-update-comments.test.js:57
    postCommentResult.ops =

  console.log test/create-update-comments.test.js:58
    [ { movieId: 573a13eff29313caabdd82f3,
        name: 'foobar',
        email: 'foobar@baz.com',
        text: 'fe-fi-fo-fum',
        date: 2019-01-21T16:02:45.739Z,
        _id: 5c45ed25c4f15027e060f731 } ]

  console.log test/create-update-comments.test.js:59
    martianComments[0] =

  console.log test/create-update-comments.test.js:60
    { _id: 5b7326fe4a68c3f7a7bcb18a,
      name: 'j8psq56jf',
      email: 'k8zrx4sl6@f598h.x69',
      movie_id: 573a13eff29313caabdd82f3,
      text: 'feefee',
      date: 2018-08-14T15:01:18.208Z }

 FAIL  test/create-update-comments.test.js (6.732s)
  Create/Update Comments
    × Can post a comment (1006ms)
    × Can update a comment (218ms)
    √ Can only update comment if user posted comment (215ms)

Ok, I just saw my typo now.

1 Like

I passed all the tests and now the frontend won’t pass.

:thinking::thinking:

Ticket: Delete Comments

again, all unit tests passed, but frontend failed.

1 Like

Hi,

Check this and see if it helps: Test shows "yes", status page says "no"

1 Like

It worked.
Thanks :grinning:

What was the typo? Having exactly the same problem. Can see the doc in the db, it does not appear in the array of comments…

@Alex_68427 You appear to have missed the discussion was started in January which means it was part of the last class and not the present one. So it’s really unlikely that person is even listening anymore.

Really the decision to leave some comment threads from previous classes open should be reviewed, since there are various problems, including of course attempting a discussion with someone who is likely not listening any more.

At the very least older helpful discussions should probably be marked read only so that no more comments like these are added.

Anyway, found it:
const commentDoc = {
name: user.name,
email: user.email,
movie_id: new ObjectId(movieId), <<< ObjectId was missing…
text: comment,
date: date
}

10 Likes

I got the test to pass. However, I want to again ask about syntax.

According to this link, the $set operator requires a field and value { $set: { field1: value1, … } }
https://docs.mongodb.com/manual/reference/operator/update/set/

However, the answer does not require the field or key. Why is that so?

What do you mean the answer does not require the field or key?

I have key value pairs in my answer.

Not really certain what that meant either, except perhaps possibly:

var  field1 = "something";

And then:

{ $set: { field } }

Which is basically expanded to:

{ $set: { field1: "something" } }

So that is possibly what @jzqc means there, more details can be read at Object initializer - New notations in ECMAScript 2015


NOTE: The actual implementation of the /status route verification does not check that updateComment() actually modifies anything, so even $set: { } would pass that test. But the unit test should fail since it’s expecting modified to show 1

Yes, that was indeed my question. Thank you @neillunn.

I am not sure about the rest of you, but for this course, I have less difficulty with MongoDB than tring to read / understand the newer JavaScript syntax!

1 Like

Please help me to Create/Update comment

@rohit114 Please don’t ask in a way that is looking for a direct “Yes/No, the code is correct” answer, as that clearly goes against the forum guidelines. In fact any potential answer meaning any guess is actually against the guidelines. And largely because people tend to copy incorrect answers as well.

Instead, if you have any error occurring from a unit test, then describe that error instead.

Really though you should be reading or simply searching ( since there is a search function on the site, which is the magnifying glass icon up the top of the screen ) for that specific error message returned. And you really should be doing that because just about all the valid reasons have already been posted.

And if you don’t have any error, then simply verify your answer and move on to the next question. It may be tempting to ask others to “green light” your answer choices, but this forum is not here for that purpose.

Besides, that’s what the unit tests are for. When they pass, then you have the correct answer.

Thanks! but I don’t rely on test cases. I have got many “green light” even my all test cases didn’t pass.

@neillunn This is my error:

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

  ● 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)
      54 |
      55 |     const martianComments = (await MoviesDAO.getMovieByID(movieId)).comments

      at toBe (test/create-update-comments.test.js:52:45)
      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: undefined

    Difference:

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

      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

  ● Create/Update Comments › Can only update comment if user posted comment

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

    Expected: 0
    Received: undefined

    Difference:

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

      84 |     )
      85 |
    > 86 |     expect(updateCommentResult.modifiedCount).toBe(0)
         |                                               ^
      87 |   })
      88 | })
      89 |

      at toBe (test/create-update-comments.test.js:86: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

  console.error src/dao/commentsDAO.js:58
    Unable to post comment: ReferenceError: text is not defined

  console.error src/dao/commentsDAO.js:85
    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:85
    Unable to update comment: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Test Suites: 1 failed, 1 total
Tests:       3 failed, 3 total
Snapshots:   0 total
Time:        3.108s, estimated 4s

This worked for me too! I’m trying to find some solid documentation on why the ObjectId wrapper is required… I guess because _id fields are of ObjectId type?

1 Like

I ran into the same problem…I wouldn’t have been able to figure it out on my own!

This was my issue as well and I completely overlooked it.

The ObjectId wrapper is needed to make the id of ObjectId type, otherwise, it will be a string. I know there’s a way to require each field in the database to a certain type. This would have been easier to identify if the app/database throws an error for having the wrong type.

@Alex_68427 thanks for pointing this out!