Answer the question
In order to leave comments, you need to log in
How can I find out if a user has not yet been added to the database?
I am using the official mongodb library.
When registering a new user, you need to find out if the id has been registered before.
However, if there is no such id in the database? FindOne method returns an error
mongo: no documents in result
u := &models.User{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := r.store.db.FindOne(ctx, bson.M{"userID": id}).Decode(u); err != nil {
return nil, err
}
return u, nil
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question