Ticket: Create / Update Comments

Help Please !!!:

I keep having this funny error message, I’ve been play around it, but can´t find the reason and the solution.

It looks the same as a previous post from Ergi_Dervishaj, he mentions his problem was : Comments not sorted the proper way, I already checked that and I am sure they are correctly sorted in descending order by date, as required in the Get Comments ticket.

Don´t understand why an unproper sort from a previous query should be a problem, this is another one, we are again accessing an indexed comments collection with _id and email fields to match up.

I tried updating comments from Mongo Shell following the same rules, no problem at all.

Any help will be truly appreciated !!!.


C:\mflix-js>npm test -t create-update-comments

server@1.0.0 test C:\mflix-js
jest --passWithNoTests “create-update-comments”

Determining test suites to run…Setup Mongo Connection
(node:20728) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
console.error
Unable to update comment: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

  86 |       return updateResponse
  87 |     } catch (e) {
> 88 |       console.error(`Unable to update comment: ${e}`)
     |               ^
  89 |       return { error: e }
  90 |     }
  91 |   }

  at error (src/dao/commentsDAO.js:88:15)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7

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

  86 |       return updateResponse
  87 |     } catch (e) {
> 88 |       console.error(`Unable to update comment: ${e}`)
     |               ^
  89 |       return { error: e }
  90 |     }
  91 |   }

  at error (src/dao/commentsDAO.js:88:15)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7

FAIL test/create-update-comments.test.js (5.348s)
Create/Update Comments
× Can post a comment (239ms)
× Can update a comment (137ms)
× Can only update comment if user posted comment (13ms)

● Create/Update Comments › Can post a comment

expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 1

- "5f763ceee23e2350f8274d01"
+ "5a9427658b0beebeb6975200"

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

  at toEqual (test/create-update-comments.test.js:57:34)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

● Create/Update Comments › Can update a comment

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

Expected: 1
Received: undefined

  68 |       date,
  69 |     )
> 70 |     expect(updateCommentResult.modifiedCount).toBe(1)
     |                                               ^
  71 |
  72 |     const movieComments = (await MoviesDAO.getMovieByID(movieId)).comments
  73 |

  at toBe (test/create-update-comments.test.js:70:47)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

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

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

Expected: 0
Received: undefined

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

  at toBe (test/create-update-comments.test.js:85:47)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:293:22)
  at Generator.next (node_modules/regenerator-runtime/runtime.js:118:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

Test Suites: 1 failed, 1 total
Tests: 3 failed, 3 total
Snapshots: 0 total
Time: 6.683s
Ran all test suites matching /create-update-comments/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.


static async updateComment(commentId, userEmail, text, date) {

try {

  // TODO Ticket: Create/Update Comments

  // Use the commentId and userEmail to select the proper comment, then

  // update the "text" and "date" fields of the selected comment.

  const updateResponse = await comments.updateOne(

    { _id: ObjectId(commentId), email: userEmail },

    { $set: { text: text, date: new Date(date) } }

  )

  return updateResponse

} catch (e) {

  console.error(`Unable to update comment: ${e}`)

  return { error: e }

}

}

¡¡ Fixed it !!.

The failure ocurred because I wasn’t wrapping the movie_id field with ObjectId() when trying to create a new comment.

6 Likes