简体   繁体   English

使用updateProfile()函数时如何检查数据库中是否已经存在邮件

[英]How to check if mail already exists in database when using updateProfile () function

I do not know how to implement a method in which when using the updateProfile () function, it checks the firestore if such mail already exists and then an error would pop up.我不知道如何实现一种方法,在该方法中,在使用 updateProfile() 函数时,它会检查 Firestore 是否已存在此类邮件,然后会弹出错误。 I did a test method in MyAccount.vue, but it doesn't work, if I don't type anything and click, nothing updates, but that's not the point, I would like it to check if such mail exists.我在 MyAccount.vue 中做了一个测试方法,但它不起作用,如果我不输入任何内容并单击,则没有任何更新,但这不是重点,我希望它检查此类邮件是否存在。

./src/views/MyAccount.vue ./src/views/MyAccount.vue

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>

./src/store/index.js ./src/store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import * as fb from '../firebase';
import router from '../router/index';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        userProfile: {},
        notes: []
    },

    mutations: {
        setUserProfile(state, val) {
            state.userProfile = val;
        },

        setNotes(state, val) {
            state.notes = val;
        }
    },

    actions: {
        async updateProfile({ commit, dispatch }, user) {
            const userId = fb.auth.currentUser.uid;

            await fb.usersCollection.doc(userId).update({
                username: user.username,
                email: user.email,
                password: user.password
            });

            dispatch('fetchUserProfile', { uid: userId });
        },

        async fetchUserProfile({ commit }, user) {
            // fetch user profile
            const userProfile = await fb.usersCollection.doc(user.uid).get();

            // set user profile in state
            commit('setUserProfile', userProfile.data());

            // change router to dashboard
            if (router.currentRoute.path === '/login') {
                router.push('/');
            }
        }
    },
    modules: {}
});

export default store;

Before updating, try this:更新前,试试这个:

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
}

Of course, this works best if you make sure to alway lowercase your emails before querying or storing them in the database当然,如果您确保在查询或将电子邮件存储在数据库中之前始终将其小写,则效果最佳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失去焦点时检查数据库中是否已存在电子邮件ID - check email id already exists in database when losing focus 如何使用PHP CodeIgniter,JQuery,AJAX检查ID验证是否已存在于数据库中 - how to Check ID verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX 如何在使用 jQuery Laravel 提交表单之前检查数据库中是否已存在记录 - How to check if a record already exists in a database before submitting the form using jQuery Laravel 使用查询 &gt; NodeJS 检查数据库中是否已存在 ROW - Check if ROW already exists in Database using query > NodeJS 如何检查具有某些字段的文档是否已存在于 MongoDB 数据库中? - How to check if document with certain fields already exists in the MongoDB database? 如何检查名称是否已存在于 Firebase 实时数据库(Firebase 和 JS)中 - How to check if name already exists in Firebase Realtime Database (Firebase & JS) 如何检查 IndexedDB 数据库是否已存在? - How do I check if an IndexedDB database already exists? 如何使用jQuery检查动态创建的元素是否已存在? - How to check if a dynamically created element already exists using jQuery? 如何使用 MERN 检查 email 是否已存在 - How do I check if email already exists using MERN 如何使用mongoose检查记录/文档是否已存在于MongoDB? - How to check if a record/document already exists in MongoDB using mongoose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM