User Management - Add a new user

Hi everyone,

When attempting to add a new user I am getting a status 200 and the UI refreshes and it appears that I am logged in because I am then able to log out:

POST /api/v1/user/register 200 275.768 ms - 453

…but when I run npm test -t user-management, this is what is returned via my terminal:

User Management
    ✕ it can add a new user to the database (209ms)
    ✓ it returns an error when trying to register duplicate user (88ms)
    ✕ it allows a user to login (25ms)
    ✓ it allows a user to logout (177ms)

 Expected value to equal:
      {"email": "", "name": "Magical Mr. Mistoffelees", "password": "somehashedpw"}
      {"email": 1, "name": 1, "password": 1, "preferences": {"favorite_cast_member": "Daniel Day-Lewis", "favorite_genre": "Drama", "preferred_ratings": ["R"]}}

I am also failing the test to login a user.

I’ve tried many different configurations, but nothing appears to be working.

I am attempting to add a new user like so (hope that it’s ok to post this here):

{ ...userInfo },

To log in a user I have tried importing BSON in the usersDAO.js, so that I can identify the JWT token with a session id like so:

{ $set: { _id: ObjectId(jwt) }},

…but that does not work. I also tried this (which is also failing):

{ user_id: email },
{$set: {jwt: jwt}}

…and not to be too long-winded, if I try to move on to the next ticket (Updating User Preferences), I am failing 1 0f the 3 tests (presumably due to the fact that my previous ticket is generating errors):

  User Preferences
    ✕ Invalid user should not have preferences (397ms)
    ✓ Null preferences should be valid (258ms)
    ✓ Valid preferences are reflected in DB (865ms)

Can anyone help me to spot what I’m either missing or doing wrong?


1 Like

You definitely appear to have done something wrong here, ans possibly have messed up your environment since during the initial addUser(userInfo) there is not any preferences data that is meant to be touched in the function.

Read the comment in the function implementation:

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

That is asking you to extract those fields from the supplied userInfo. That is not what { ...userInfo } with a spread operator does. In fact it’s the reverse of what you want.

Pretty much the same case goes for all other functions. READ the implementation comments, as they do tell you exactly what is expected. The lab is also somewhat simple when compared to others since all data passed in are “strings” and require no other conversion or type casting, as other lab questions DO require. Not here though.

As for your current results in the test. I would strongly recommend restoring fresh data from the supplied backup data for the course, AND just in case you actually also altered the code in the unit tests ( as persisted data that should not be there would indicate has been done ) then extract those original files from the supplied course archive as well.

Hi @neillunn,

I DID read (more than once) the implementation comments and (initially) I tried to add a user like so:

     password: userInfo.password

I also tried:

     name: name,
     email: email,
     password: password


     name: 1,
     email: 1,
     password: 1

…and none of these things worked, so out of desperation, I attempted to use the spread operator.

I’m missing something and there appears to be no way to figure what I’m doing wrong at the moment without having someone actually show me what I’m missing.

Thanks for taking the time to respond:-)

1 Like

The worry here is exactly your last example ( with the email: 1 etc ) which is just how the data is showing in the test result, and along with the preferences field which was stated should not be there.

I say worry as it indicates you have done something to your “data” which has broken things, and you need to fix that. If you have created a problem in your data, then “fixing code” is of little consequence.

I might also suggest some reading of the examples at: Destructuring assignment - MDN

I have refreshed my data by running mongorestore --drop..... I have also replaced all of the test (ie., tickets) with a fresh download.:grinning:

Now I am receiving an undefined error when trying to sign up a new user (after having once again tried all of the various configurations above). Are any of my configurations above even close?

I haven’t done anything to my data, other than trying to create a user(s) via the UI, but I did do a mongorestore as you have suggested.

Thanks for the link. I’ll check it out. In the relatively short time (3-4 years) that I’ve been learning to code JS (Angular, React, Vanilla JS, etc…) has changed enough times to make my head spin, sometimes I still find myself stumbling a bit.:rofl:

Thanks again

hi @juliettet, consider adding write concern to your first query and try again.

Hi @James_22203,

The writeConcern has been there the whole time. I probably should have mentioned that for the sake of clarity, but I didn’t want to post everything in my code here to avoid trouble:-)


YES. Without actually spelling out answers I can say that, but the “Destructuring” link should give you more than enough information to determine what to do.

The MAIN concern I mentioned both times was the presence of preferences in the data from the very first test result that should not be there.

The reasoning behind the “refresh” here was basically due to either:

  1. You just managed to implement more things in the function than you were actually asked to do. So not so much a data problem but a coding problem.

  2. At some point you wrote errant data which is still being read, and this would point to a “tear down” in the unit tests not working because something got changed.

Basically, all data you insert in the tests should get destroyed after each test is run. So getting output that has nothing to do with the input is a real problem.

FYI, the “UI” as it were ( ie Browser Front End ) is actually not part of the course requirement in any way. In order to pass the graded assignments you don’t need any UI interation at all, other than hitting the browser refresh button to reload the /status route ( I m ean how hard would it have been to reload the client on server change detected! - Course devs take note ).

If you see a specific error, then include that in the thread. But note “if” you are looking in the “browser console”, then that’s the wrong place to look. This is almost exclusively back end implementation all the way.

@James_22203 - I think if you take into context the sort of questions the poster is asking here, then “write concern” ( which the question does NOT ask you to do, and is not required ) is just confusing the issue. The only comment on the function implementation is actually for a different lesson and ticket.