User Management: duplicate emails should not be allowed [Solved]

Please, suggest how to manage mentioned in topic message.
Tests are fine. However, in console I see this one exception message:


double check on:
db.users.count()
db.users.distinct(“email”).length
returns the same values.
I can catch exception in code. But I cannot identify root cause.

Hi @Igor_48396,

These errors will be attended in labs ahead.
If you get the unit tests to pass and the status page to provide the validation code, you are good.

N.

I got it. Thank you Norberto.

Even when I thought that that kind of error should be taking care of on other tickets, the current ticket can’t pass because of this, and that error message is displayed.

Can you put a photo of all the results? Not all of them have to be green to pass to the next lab.

All of my results before that are passed, the thing here is that there’s an error thrown by my application dictating that there’s a MongoWriteException thrown, because of a duplicated key, but the application doesn’t know how to manage it.

public boolean addUser(User user) throws IncorrectDaoOperation {
usersCollection.insertOne(user);

return true;

}

And the error is:

com.mongodb.MongoWriteException: E11000 duplicate key error collection: mflix.users index: email_1 dup key: { : "gw1gnj0h1@62aki.yce" }
1 Like

Hi @vanfigo

Typically you want an application to throw an error when the same data (by key) is entered. If you need to hand this you can use a try/catch block for a MongoWriteException but this isn’t necessary for these labs.

Hope this helps!
Eoin

It does, the only thing is that if I handle the exception, the status page will hold forever, without sending any message. So I’m kind of stuck either receiving a “duplicate” error message or a “still validating” message. I decided to move forward and skip that single ticket.

1 Like

You have to validate if the email exist. If no, you can “insertOne”. Something like this:

On the other hand, to achieve that all of my tests in “UserTest.java” were successful, in the method “deleteUser” I had to call the method “deleteUserSessions”.

2 Likes

You should always try to avoid multiple round trip to the database as this is slower.

It is more efficient to try the operation and catch the exception. Also it is entirely possible that, between your call to getUser() and the call to insertOne() (since it is 2 database accesses), another client insert the same user. So your code will still fails with the getUser() check.

Yes, you are totally right.

Hi @vanfigo,

i had the same problem, but i found 2 answers in forum for it.

See here:

And here: