Basic Writes: Failing in loginUser

I’m failing in loginUser…

This is my code :

static async loginUser(email, jwt) {
try {
// TODO Ticket: User Management
// Use an UPSERT statement to update the “jwt” field in the document,
// matching the “user_id” field with the email passed to this function.
user_id_login = getUser(email).user_id;

  await sessions.updateOne(
    { user_id: user_id_login },
    { $set: { jwt: jwt } },
  )
  return { success: true }
} catch (e) {
  console.error(`Error occurred while logging in user, ${e}`)
  return { error: e }
}

}

I get this:

What I do is get the user_id using the getUser function, and then, use this user_id found to update only this one.

I’ll apreciate any help.

Thanks !!

The key part that you missed is written in upper case in the requirement:

Hi Steve,

Do you mean this?

await sessions.updateOne(
{ user_id: email},
{ $set: { jwt: jwt} },
{ upsert: true }

Thanks !!

With this code I get this error:

I’m encountering the same problem.
With this code

static async loginUser(email, jwt) {
    try {
      // TODO Ticket: User Management
      // Use an UPSERT statement to update the "jwt" field in the document,
      // matching the "user_id" field with the email passed to this function.
      await sessions.updateOne(
        { user_id: email},
        { $set: { jwt: jwt} },
        { upsert: true },
      )
      return { success: true }
    } catch (e) {
      console.error(`Error occurred while logging in user, ${e}`)
      return { error: e }
    }
  }

I’m getting this error:

User Management › it allows a user to login

TypeError: Cannot convert undefined or null to object

  50 |     expect(actual.success).toBeTruthy()
  51 |     const sessionResult = await UsersDAO.getUserSession(testUser.email)
> 52 |     delete sessionResult._id
     |     ^
  53 |     expect(sessionResult).toEqual(sessionUser)
  54 |   })
  55 | 

  at _callee5$ (test/user-management.test.js:52:5)
  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: 1 failed, 3 passed, 4 total
Snapshots: 0 total
Time: 4.416s
Ran all test suites matching /user-management/i.
Teardown Mongo Connection

@xgubianas you need to close the parenthesis after {upsert:true}, before the return statement.

@Sergei_78363 your issue isnt with the loginUser function, it is with the getUserSession.

static async getUserSession(email) {
try {
  // TODO Ticket: User Management
  // Retrieve the session document corresponding with the user's email.
  return sessions.findOne({ user_id: email })
} catch (e) {
  console.error(`Error occurred while retrieving user session, ${e}`)
  return null
}

}