Javascript 如何在使用updateProfile()函数时检查数据库中是否已经存在邮件
我不知道如何实现一种方法,在使用updateProfile()函数时,它会检查firestore是否已经存在这样的邮件,然后会弹出一个错误。我在MyAccount.vue中做了一个测试方法,但它不起作用,如果我不键入任何内容并单击“无任何更新”,但这不是重点,我希望它检查是否存在此类邮件 ./src/views/MyAccount.vueJavascript 如何在使用updateProfile()函数时检查数据库中是否已经存在邮件,javascript,firebase,vue.js,google-cloud-firestore,Javascript,Firebase,Vue.js,Google Cloud Firestore,我不知道如何实现一种方法,在使用updateProfile()函数时,它会检查firestore是否已经存在这样的邮件,然后会弹出一个错误。我在MyAccount.vue中做了一个测试方法,但它不起作用,如果我不键入任何内容并单击“无任何更新”,但这不是重点,我希望它检查是否存在此类邮件 ./src/views/MyAccount.vue import { mapState } from 'vuex'; export default { data() { return
import { mapState } from 'vuex';
export default {
data() {
return {
user: {
username: '',
email: '',
password: ''
}
};
},
computed: {
...mapState(['userProfile']),
},
methods: {
updateProfile() {
this.$store.dispatch('updateProfile', {
username:
this.user.username !== ''
? this.user.username
: this.userProfile.username,
email:
this.user.email !== ''
? this.user.email
: this.userProfile.email,
password:
this.user.password !== ''
? this.user.password
: this.userProfile.password
});
this.user.username = '';
this.user.email = '';
this.user.password = '';
this.showSuccess = true;
setTimeout(() => {
this.showSuccess = false;
}, 2000);
}
}
};
</script>
在更新之前,请尝试以下操作:
const current = await fb.usersCollection.where('email', '==', user.email).get()
if (current.empty === true) {
// You are free to do the update, because the email is not in use already
}
当然,如果您在查询或将电子邮件存储在数据库中之前确保始终将其小写,则此方法效果最佳谢谢Simon。它正在工作。但我有一个问题。因为这只适用于电子邮件,如果我想同时验证用户名和电子邮件呢?我必须进行这样的查询2x?是的,这将是两个单独的查询,因为Firestore在查询性能优化时不使用OR。所以,如果用户名必须是唯一的,那么您需要一个额外的查询来测试它。
const current = await fb.usersCollection.where('email', '==', user.email).get()
if (current.empty === true) {
// You are free to do the update, because the email is not in use already
}