簡體   English   中英

使用updateProfile()函數時如何檢查數據庫中是否已經存在郵件

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

我不知道如何實現一種方法,在該方法中,在使用 updateProfile() 函數時,它會檢查 Firestore 是否已存在此類郵件,然后會彈出錯誤。 我在 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

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;

更新前,試試這個:

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
}

當然,如果您確保在查詢或將電子郵件存儲在數據庫中之前始終將其小寫,則效果最佳

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM