简体   繁体   English

Nuxt JS / Firebase 的 Vuex 未知操作

[英]Vuex unkown action with Nuxt JS / Firebase

I'm trying to implement an authentication system in Nuxt JS 2.9.2, and Firebase 6. I'm using the Nuxt Fire module, and Vuexfire module.我正在尝试在 Nuxt JS 2.9.2 和 Firebase 6 中实现身份验证系统。我正在使用 Nuxt Fire 模块和 Vuexfire 模块。

I've successfully setup the system where a user can be created, logged in, and logged out, as well as saving some data to the real time database, however, I'm having some troubles trying to retrieve data from my real-time database.我已经成功设置了可以创建、登录和注销用户并将一些数据保存到实时数据库的系统,但是,我在尝试从实时数据库中检索数据时遇到了一些麻烦数据库。

I'm trying to pull out information from my real-time database based on the user currently logged in, eg:我正在尝试根据当前登录的用户从我的实时数据库中提取信息,例如:

store/localStorage.js商店/本地Storage.js

import firebase from 'firebase'
import { firebaseAction } from 'vuexfire'

export const state = () => ({
  user: null,
  account: null
})

export const actions = {

  setAccountRef: firebaseAction(({ bindFirebaseRef }, path) => {
    return bindFirebaseRef('account', firebase.database().ref(path))
  })

}

export const mutations = {

  /*
   * Set user information
   */
  setUser (state, payload) {
    state.user = payload
    return this.dispatch('setAccountRef', `accounts/${state.user.uid}`)
  }

}

Unfortuantly, my setup appears to give me a: [vuex] unknown action type: setAccountRef error.不幸的是,我的设置似乎给了我一个: [vuex] unknown action type: setAccountRef错误。

For your information, setUser is ran from another module file:为了您的信息, setUser是从另一个模块文件运行的:

store/authentication.js商店/authentication.js

createUser ({commit}, payload) {
    this.$fireAuth.createUserWithEmailAndPassword(payload.email, payload.password).then(function({user}) {
      commit('localStorage/setUser', { email: payload.email, uid: user.uid }, { root: true })
      createNewAccount(user)
      $nuxt.$router.push({ path: '/', query: {
        alertActive: true,
        type: 'info',
        message: 'Your account has been successfully created'
      }})
    }).catch(function(error) {
      console.log('error registering' + error)
    });
  }

What am I missing in terms of the unknown action?就未知操作而言,我缺少什么?

General一般的

I would advise you to never don't blow up your mutations, mutations are meant to only update the local state .我建议你永远不要破坏你的突变,突变意味着只更新本地状态 They also cannot be asynchronous.它们也不能是异步的。

You cannot access dispatch in mutations anyway.无论如何,您无法在突变中访问dispatch You only get the state and the payload as parameters.您只能获取状态和有效负载作为参数。

Read more in the official vuex documentation .官方 vuex 文档中阅读更多内容

Solution for your problem:您的问题的解决方案:

Create an action called setUser(), there you can access dispatch like so:创建一个名为 setUser() 的操作,在那里您可以像这样访问 dispatch:

export const actions = {

  setAccountRef: firebaseAction(({ bindFirebaseRef }, path) => {
    return bindFirebaseRef('account', firebase.database().ref(path))
  }),

  setUser: ({commit, dispatch}, payload) => {
    commit('SET_USER', payload)
    return dispatch('setAccountRef', `accounts/${payload.uid}`)
  })

}

export const mutations = {

  /*
   * Set user information
   */
  SET_USER: (state, payload) => {
    state.user = payload
  }

}

Hope that helped.希望有所帮助。

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

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