I have next scenario:
if (username exists in database)
throw new Error(msg1);
if (email exists in database)
throw new Error(msg2);
insertNewPlayer(username, email, password);
My environment is nodejs with TypeScript and I am using RxJS and MongoDB driver for nodejs. And I wrote next code:
return Observable.fromPromise(this.players.findOne({username: username}))
.mergeMap(data => {
if(data) throw new Error("That username is taken!");
return Observable.fromPromise(this.players.findOne({email: email}));
})
.mergeMap(data => {
if(data) throw new Error("That email is taken!");
return Observable.fromPromise(this.players.insert(playerData));
})
.map(result => this.createNewPlayerFromData(playerData))
Since I am using MongoDB driver for nodejs I need to use .fromPromise so I can "cast" it to Observable...
Is this the correct way to do this? Or is there any other way?
(Do not worry about createNewPlayerFromData function, it's just transforms playerData object to object of type Player)
findOneinto one query (has this user name or has this email) or run them in parallel? Also, I will assume there are other operators after the lastmapbecause if you don'tsubscribefrom the observable, it won't run past thethis.players.findOne({username: username})promise.findOnebut I need to know is username or email that is not unique, so I can show appropriate message to user.