There are 2 collections in my mongoDB:
1. collection which contains user phone number and wwhether it is verified or not? 集合?
{
_id: '12123',
phones: [
{
phoneNumber: '1234567890',
verified: false
}
]
}
2. collection which contains the verification code mapped by id from user collection as . 集合,其中包含由用户集合中的id映射为的验证码。
{
_id: '1111',
userId: '12123',
token: '4545'
}
I'm creating an endpoint in Spring WebFlux to verify the phone number. The endpoint receives the and . 和 。 If the token in the collection matches the token sent by the user, then update the verified to true in user collection.
I'm trying to write a single function that would be called by this endpoint and make the required changes.
I tried in the following code but the verified status is not updating to true.
public Mono<VerifyPhoneToken> verifyPhoneNumber(String id, String verificationCode) {
return verifyPhoneTokensRepository.findByUserId(id)
.flatMap(vpt -> {
if (verificationCode.equals(vpt.getToken())) {
usersRepository.findById(id)
.flatMap(user -> {
user.getPhones().get(0).setVerified(true);
return usersRepository.save(user);
});
return verifyPhoneTokensRepository.save(vpt);
}
return null;
});
}
Also, I would like to know if return null
can be handled in a better way.
im guessing your problem is when you are calling the usersRepository
you are not handling the return thus breaking the event chain. You should try something like:
public Mono<VerifyPhoneToken> verifyPhoneNumber(String id, String verificationCode) {
return verifyPhoneTokensRepository.findByUserId(id)
.flatMap(vpt -> {
if (verificationCode.equals(vpt.getToken())) {
return updateUser(id)
.then(verifyPhoneTokensRepository.save(vpt));
}
return Mono.empty();
});
}
private Mono<User> updateUser(String id) {
return usersRepository.findById(id)
.flatMap(user -> {
user.getPhones().get(0).setVerified(true);
return usersRepository.save(user);
});
}
using then
to chain on your action. Also dont return null
return Mono<Void>
(Mono.empty())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.