简体   繁体   English

Vuex状态不更新

[英]Vuex state not updating

I am trying to render different UIs depending on whether a user is a retailer or not. 我试图根据用户是否是零售商来呈现不同的UI。 I think I have mostly everything correct, but when the app initializes, I want to check whether the user is a retailer or not (This dispatches the checkAuth action). 我认为我的一切都是正确的,但是当应用初始化时,我想检查用户是否是零售商(这将调度checkAuth操作)。

I try running the following to get the value of the isRetailer property from Firebase. 我尝试运行以下isRetailer从Firebase获取isRetailer属性的值。

firebase.database().ref('/users/' + user.uid + '/isRetailer').once('value').then(snapshot => snapshot.val()

Then I dispatch some actions based on the result. 然后我根据结果发送一些动作。 For some reason, the isRetailer part of my Vuex state is not updating from false even when the users isRetailer property is true. 出于某种原因,即使用户的isRetailer属性为true,我的Vuex状态的isRetailer部分也不会从false更新。

I'm not really sure why but I'm guessing it has something to do w/ my firebase reference. 我不确定为什么,但我猜它有什么事情要做我的firebase参考。 My full code is below. 我的完整代码如下。 Thanks! 谢谢!

import firebase from 'firebase'

const authentication = {
    state: {
        isAuthed: false,
        authId: '',
        isRetailer: false
    },
    mutations: {
        authUser (state, user) {
            state.isAuthed = true;
            state.authId = user.uid;
        },
        notAuthed (state) {
            state.isAuthed = false;
            state.authId = '';
            state.isRetailer = false
        },
        isRetailer (state) {
            state.isRetailer = true
        },
        isNotRetailer (state) {
            state.isRetailer = false
        }
    },
    actions: {
        checkUser (context, user) {

            if (!user.uid) {

                // Do nothing.

            } else if (firebase.database().ref('/users/' + user.uid + '/isRetailer').once('value').then(snapshot => snapshot.val()) === true) {

                context.commit('authUser', user);

                context.commit('isRetailer');

            } else {

                context.commit('authUser', user);

                context.commit('isNotRetailer');
            };
        },
        signOut (context) {
            context.commit('notAuthed')
        },
        setRetailer (context, payload) {

            // Set retailer info in Firebase.

            var uid = payload.user.uid;
            firebase.database().ref('/users/' + uid).set({
                name: payload.name,
                email: payload.email,
                location: payload.retailerLocation,
                isRetailer: payload.isRetailer
            });  

        },
        setNewUser (context, payload) {

            // Sets a user in firebase w/ a isRetailer value of false. 

            var uid = payload.user.uid;

            firebase.database().ref('/users/' + uid).set({
                name: payload.name,
                email: payload.email,
                isRetailer: payload.isRetailer
            });
        }
    },
    getters: {}
};

export default authentication;

This might be happening because you have given same name to mutation and state variable: isRetailer , I don't have any reference right now, but this might be the case for mutation not triggering, You can try once by setting your mutation by different name such as: 这可能会发生,因为你给变异和状态变量命名: isRetailer ,我现在没有任何引用,但这可能是突变不触发的情况,你可以尝试一次通过不同的名称设置你的突变如:

mutations: {
    ...
    ...
    setIsRetailer (state) {
        state.isRetailer = true
    },
    isNotRetailer (state) {
        state.isRetailer = false
    }
},

and use this in actions: 并在动作中使用它:

actions: {
    checkUser (context, user) {
        ...
        ...

            context.commit('authUser', user);
            context.commit('setIsRetailer');

        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM