Hey everyone. I need help. I have been trying to solve this ticket for 4 hours now, and I am losing hope.
Whenever I try to implement the loginUser
and addUser
tickets I run into roadblocks
Here is my addUser()
implementation
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.
let { name, email, password } = userInfo;
console.log(`${name}, ${email}, ${password}`)
await users.insertOne(
{
name: name,
email: email,
password: password
})
// TODO Ticket: Durable Writes
// Use a more durable Write Concern for this operation.
await users.insertOne({ someField: "someValue" })
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 }
}
}
Here is my loginUser()
implementation.
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 },
)
} catch (e) {
console.error(`Error occurred while logging in user, ${e}`)
return { error: e }
}
}
This little part is beyond me. I have gone through countless posts on the forums but none seem to relate to the errors I am getting.
Here are the errors for each of the implementations respectively:
â 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/babel-runtime/node_modules/regenerator
-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (node_modules/babel-runtime/nod
e_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/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
â User Management âș it allows a user to login
TypeError: Cannot read property 'success' of undefined
48 | test("it allows a user to login", async () => {
49 | const actual = await UsersDAO.loginUser(testUser.email, sessionUser.jwt)
> 50 | expect(actual.success).toBeTruthy()
| ^
51 | const sessionResult = await UsersDAO.getUserSession(testUser.email)
52 | delete sessionResult._id
53 | expect(sessionResult).toEqual(sessionUser)
at success (test/user-management.test.js:50:19)
I will greatly appreciate any guidance on the above.