User Management

Hello,

I understand that this question has been asked so many times you must be sick of seeing and answering it. But having read all the posts I can find with reference to this Ticket, I am still unsure of how I am failing the addUser test.

I can’t figure out how to get my code to be a “truthy”:

/**
* Adds a user to the `users` collection
* @param {UserInfo} userInfo - The information of the user to add
* @returns {DAOResponse} Returns either a "success" or an "error" Object
*/
static async addUser(userInfo) {
  /**
  Ticket: Durable Writes
  Please increase the durability of this method by using a non-default write
concern with ``insertOne``.
  */
  try {
    // TODO Ticket: User Management
    // Insert a user with the "name", "email", and "password" fields.
    // TODO Ticket: Durable Writes
    // Use a more durable Write Concern for this operation.
    await users.insertOne({ name: userInfo.name }, {email: userInfo.email}, { password: userInfo.password })
    return { success: true }
  } catch (e) {
    if (String(e).startsWith("MongoError: E11000 duplicate key error")) {
    return { error: "A user with the given email already exists." }
  }
  console.error(`Error occurred while adding new user, ${e}.`)
  return { error: e }
  }
}

When the test is ran I get the following failure:

 FAIL  test/user-management.test.js
User Management
× it can add a new user to the database (44ms)
√ it returns an error when trying to register duplicate user (28ms)
√ it allows a user to login (73ms)
√ it allows a user to logout (55ms)

* User Management › it can add a new user to the database

expect(received).toBeTruthy()

Received: undefined

  29 |      */
  30 |     const actual = await UsersDAO.addUser(testUser)
> 31 |     expect(actual.success).toBeTruthy()
     |                            ^
  32 |     expect(actual.error).toBeUndefined()
  33 | 
  34 |     // we should be able to get the user

  at toBeTruthy (test/user-management.test.js:31:28)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97: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:       1 failed, 3 passed, 4 total
Snapshots:   0 total
Time:        2.324s
Ran all test suites matching /user-management/i.
Teardown Mongo Connection
npm ERR! Test failed.  See above for more details.

As the response is “undefined” the addUser function is not returning a success or an error… it looks as if the insertOne function is falling over, but I don’t understand the how/where or why.

The error is in the following:

You are passing 3 parameters (3 documents) to insertOne. You should be passing one document that has 3 fields. You have too many braces.

2 Likes

Omg… now that you’ve pointed that out I’ve done an epic facepalm.

Thank you steevej-1495 for point out such a silly thing!