Ch. 2: UserTest testLogin Duplicate Key error

Whenever I run the UserTest on the testLogin, I’m getting back an E11000 duplicate key error for gryffindor@hogwarts.edu. I know that is used in the setup of the test. Regardless, I can’t get past this. It is throwing this within the createUserSession. Within this, I’m simply have
Session session = new Session();
session.setJwt(jwt);
session.setUserId(userId);
sessionsCollection.insertOne(session);

I know that this error will occur if there is a duplicate, how do I move past this? How do I remove this after testing?

Hi @Clinton_68637,

This can be easily fixed by dropping the sessions collection in the Atlas UI or via the mongo shell.

Just run the following:

mongo "mongodb+srv://<YOUR_SERVER>/mflix" --username m220student --password m220password --eval 'db.sessions.drop()'

N.

1 Like

I did and it returned “false”.

first use method getUserSession(userId)
if null - create new one, if not null - return true

I tired to do this, but it tell me “insertOne (org.bson.Document) n MongoCollection cannot be applied in (mflix.api.models.Session)” why it it?

It looks like the declaration of sessionsCollection may have been changed. Please share the declaration.

In UserDao you should find the following:

private final MongoCollection<Session> sessionsCollection;

Does it make sense now why you are getting that error?

N.

1 Like

Thank you. solved it out