Create, Update, Delete Comments

Hi @Carlos_11233,

I’m still stuck in stuckland. If I run the get comments pipeline via Compass with ObjectId("573a1390f29313caabcd418c"), I can see an array of 2 comments returned with the first one having the date/year 2012 and the 2nd comment w/ the date/year of 2011. So it appears that my sort by date functionality is indeed working.

I tried destructuring for the name and email fields, but that doesn’t appear to be working…not sure if there is an issue there.

I must be crazy for trying to sort this out (pun intended) at this point.

Thanks for taking the time to help :smiley:

Update:

Although I realize that this is strictly a backend course, I noticed that there is a 401 not authorized error on the comments route when I refresh the app in my browser:

POST /api/v1/movies/comment 401 0.414 ms - 64

This appears to to be the problem, but I’m not sure how to fix it.

You receive a 401 - Unauthorized basically because addUser() is not correctly implemented. The longer explanation is there is a /register route which is called first by the front end and this depends on addUser() being implemented correctly.

You really should take your attention away from the /status page results until you have the create-update-comments.test.js actually passing without errors, since the front end interactions are naturally more complex and with more dependencies. Please also discount ANY claims that the /status page is meant to pass even if these tests fail, as that statement is simply untrue.

Just to give you a concise checklist of all things that have already been covered in various other threads:

  1. You must have the get-comments test PASSING before you move on to this implementation. The tests depend on getMovieByID() to be implemented correctly. ie Get the movie detail, “join” the comments and sort the comments only so they are presented with the most recent comment first.

  2. The user-management test must be passing, as several tests ( though mostly from front end ) require this is a dependency in order to pass from that point forward. i.e Presume that every test after that one relies on it being implemented properly. Moreover, you should ensure if you have not already that you do not skip ahead and attempt to answer any section later than one that has not been solved.

  3. Ensure you have a sufficient timeout value for the Jest tests. Errors can present as either a specific messgage including jest.setTimeout or as claiming a Promise has not completed. ( People have posted the common variants of this error in posts here already ). You can set this globally as already noted on a previous thread.

  4. Implement the actual addComment() function. The key things to remember here are:

    • The movie_id is the foreign key to the movies collection and it’s own _id field which must be an ObjectId type. There is example code already in the DAO section that shows you how to take the “string” ( which is all API input ) and turn that into an ObjectId.

    • The method arguments addComment(movieId, user, comment, date) are NOT an exact match for the field names in the collection. You should look at the comments collection and determine the actual field names, but user is an Object with multiple properties and you need only two of those in order to match the expected document structure. Also comment is text in expected field name for a collection.

  5. Object properties can be accessed very simply. For example:

     var foo = { a: 1, b: 2, c: 3 };
    
     console.log(foo.a);     // returns 1
     console.log(foo.c);     // returns 3
    

    If you need to just refer to individual values of user, then you can pretty much do that. There are other ways, but a correct implementation does not require anything complex. Simply the expected values.


Note: Where the addUser() is still failing, the comment in the function helps:

// Insert a user with the "name", "email", and "password" fields.

So does looking at the data used by the unit test for user-management:

const testUser = {
  name: "Magical Mr. Mistoffelees",
  email: "magicz@cats.com",
  password: "somehashedpw",
}

This is an Object with exactly the same fields as you being asked to insert.

From the above foo example, if I wanted to insert a “document” with the "fields "a", "b" and "c" into the baz collection, I would simply do:

db.collection("baz").insertOne(foo);

Because foo is already an Object, just like the insertOne() method is expecting, as well as the expected structure of the document for the collection.


In the event you believe your addUser() method is correctly implemented and you have all tests passing ( both the unit tests for user-management and the /status page entry ) then the only cause left is that something has been changed in the controller implementation, or elsewhere outside of the DAO section which is the only place changes should be made.

With nothing left than the possibility that something there has been changed, it would be advisable to download the application handout again and clean install to a separate directory. You would be required to reapply any edits to the DAO code up to the current point, but this would ensure the code that was not meant to be touched has not been altered in any way.

In the event of a clean install, it would also be recommended to enable source control ( git or other ) on the new code install, to ensure that any other inadvertent or accidental changes can be easily identified and/or rolled back where required.

1 Like

After wrapping movieId and commentId with ObjectId all my tests have passed

5 Likes

Hi @neillunn,

Thank you for this very well thought out and informative post. I was just reading this very post on another thread and I have gone through the checklist that you have provided and here are the results so far:

  1. My get-comments test is passing and I can see (via Compass) that the comments are sorted from newest to oldest.

  2. The only issue that I’m having with the user-management ticket at this point is in adding a user, the other 3 tests on that ticket are passing.

  3. I have (thanks for the tip!) created a jest.setup.js file at the root of the mflix directory with the timeout of 30000 (jest.setTimeout(30000)).

  1. In the addComment() function, I have made sure that movieId is of type ObjectId.

  2. I have all of the correct field names that correspond with the addComment() arguments. I’ve checked, and double/triple checked the comments collection via the Mongo shell.

  3. “Because foo is already an Object, just like the insertOne() method is expecting, as well as the expected structure of the document for the collection.” Thank you for the tip! This is very helpful. This is precisely what I am doing now (ie., instead of trying to insert an object like so: { a: 1, b:2 }).

So it appears that I have implemented everything (at least with regards to the addUser() function) correctly.

I’ll try a fresh install of the handouts (again). I have also re-uploaded the data via mongorestore several times…just in case.

I thought about starting a Github repo for this project, but I was reluctant to do so as I’m sure that Mongo U wouldn’t appreciate the course files (answers, etc…) being visible via a Google search, but in hindsight, I could create a private repo.

…the only other thing can think of is that I need to experiment with the setTimeout value in jest.setup.js.

If I can get this to work I am going to be jumping for joy!

Thanks again. :smile:

Update:

The user-management tests are now working!!!

As suspected, most of my issues (or really THE issue) was with my failing addUser() function. Now that I’ve got that working, I am able to pass the test(s) for create-update-comments and delete-comments are now passing. I also had a mistake/typo in my getUser() function…

OMG! I can’t believe it!!

Thanks to everyone for their help.

…now on to the user-report ticket…

2 Likes

I managed to solve the test after I read and understood the need for ObjectID
In compass if you want to find an object after id you have to write: {_id: ObjectId (“5a9427648b0beebeb69579cc”)}
The explanation is “MongoDB Extended JSON
mmt

1 Like

Hi juliettet,
I can’t get the get comments working. would you give me some hints. Thanks!

Hi @Nu_81142,

Are you referring to the pipeline to get comments in the getMovieByID(id) function in moviesDAO.js?

If so, have you tried building your pipeline in Compass first to see if you can retrieve comments by date (in descending order)?

That worked for me, but without that, the STATE page showed me that everything was fine as Carlos_11233 mentioned before

Yes @juliettet,

it keeps complains the word “let” , no matter what I did, “let”, ‘let’.

When I read your comment I test the status page and wow, It worked for me too, then I wrapped the movie_id in the ObjecteId() and the others 2 test passed ok. Nailed it

2 Likes

That works fine, you have to add first the match stage, then the lookup stage and thats it. Remember to use the example in the video just with the params: from, let, pipeline and as. That’s enough to help you!

Hmm… that’s strange…

We should (as not to confuse anyone that may visit this thread in the future) continue this over here:

PS. Adding to what @ canelacho has pointed out, don’t forget to add a sort stage.

Try that out and I’ll look for you on the other thread to see if you’re still stuck.

1 Like

I passed all the tests for delete comments, but the integration (status page) test is failing. I’m getting this message in the on the screen: Delete Comments: Unable to retrieve movie comments

and in the browser console I’m getting 401 and 500 level errors, has anyone else passed the test cases but not passes the integration test?

I was encountering the same issue until I wrapped movieId & commentId with the ObjectID() function. :sweat_smile:

const commentDoc = {
name: user.name,
email: user.email,
movie_id: ObjectId(movieId),

    text: comment,
    date: date
  }

  return await comments.updateOne({ movie_id: ObjectId(movieId) },
    { $set: commentDoc }, { upsert: true })

unable to insert a comment please help me in sorting out this issue.

For update wrote below code
const updateResponse = await comments.updateOne(
{
email: userEmail,
movie_id: ObjectId(commentId),
date: date
},
{ $set: { text: text } },
)

Please do not post wildly across the forums with the same question. I’ve answered your question in another thread. Reposting the same issue multiple times only leads to clutter the forums.

Updating a comment fails
let dates = new Date();
const updateResponse = await comments.updateOne(
{
email: userEmail,
movie_id: ObjectId(commentId),
date: date
},
{ $set: { text: text, date: dates } },
)

Resolved the above issue, id i should point to _id instead movie_id


Test case passed but status code returned by those service is 500

please help me in sorting out this issue please

Hi @sathya_narayanan_24539,

There are still a couple of (small) issues that need to be resolved:

  1. You only need to select the comment with the commentId and userEmail parameters (and nothing else).

  2. Check the spelling of the assigned parameters that follow $set.

Try implementing these steps and the /api/v1/movies/comment 500 internal service error should go away.

Hope this helps. :smile: