简体   繁体   English

无法读取未定义的属性“电子邮件”; firebase auth(); 使用 Vuex

[英]Cannot read property 'email' of undefined; firebase auth(); using Vuex

I am using vuex to store the logged in users email but when I try to commit the mutation which stores this email to a state object it throws this error Cannot read property 'email' of undefined . I am using vuex to store the logged in users email but when I try to commit the mutation which stores this email to a state object it throws this error Cannot read property 'email' of undefined .

//this is my action

    const actions = {
      login({ commit }, context, payload) {
        firebase
          .auth()
          .signInWithEmailAndPassword(payload.email, payload.password)
          .then(user => {
            let payloadUserEmail = user.user.email;
            console.log(payloadUserEmail);
            commit("SET_USER", payloadUserEmail);
          });
      }
    };

//this is my mutation

const mutations = {
  SET_USER(state, email) {
    state.userLoggedEmail = email;
    state.isLoggedIn = true;
  }
};


//this is my state

const state = {
  userLoggedEmail: "",
  isLoggedIn: false
};

I always gives me this error when I try to commit the logged in user to state but when I console.log it out it shows.当我尝试将登录用户提交给 state 时,我总是给我这个错误,但是当我 console.log 它显示它时。

//this is my login component 
<template>
  <div class="container-fluid">
    <div class="row">
      <div class=" col-sm-10 col-md-6 col-lg-4 mx-auto mt-4">
        <div class="card">
          <div class="card-header bg-dark">
            <span style="font-weight: bold; opacity: 1; color: white;"
              >Login</span
            >
          </div>
          <div class="card-body">
            <div class="form-group">
              <label>Email</label>
              <input
                type="email"
                class="form-control"
                id="newStockName"
                placeholder="Enter Email"
                v-model="user.email"
              />
            </div>
            <div class="form-group">
              <label>Password</label>
              <input
                type="password"
                class="form-control"
                id="newStockPrice"
                placeholder="Enter Password"
                v-model="user.password"
              />
            </div>
            <button
              type="submit"
              class="btn btn-primary btn-success btn-block"
              @click="emailLogin()"
            >
              Login
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { mapActions } from "vuex";

export default {
  data() {
    return {
      user: {
        email: "",
        password: ""
      }
    };
  },
  name: "Login",
  methods: {
    emailLogin() {
      this.$store.dispatch("auth/login", {
        email: this.user.email,
        password: this.user.password
      });
    }
  }
};
</script>

This is what the error message shows it has two errors:这是错误消息显示它有两个错误的内容:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'email' of undefined"

found in

---> <Login> at src/components/authentication/Login.vue
       <App> at src/App.vue
         <Root>
warn @ vue.runtime.esm.js?2b0e:619
logError @ vue.runtime.esm.js?2b0e:1884
globalHandleError @ vue.runtime.esm.js?2b0e:1879
handleError @ vue.runtime.esm.js?2b0e:1839
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1862
invoker @ vue.runtime.esm.js?2b0e:2179
original._wrapper @ vue.runtime.esm.js?2b0e:6917
vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'email' of undefined
    at Store.login (auth.js?c7d4:17)
    at Array.wrappedActionHandler (vuex.esm.js?2f62:847)
    at Store.dispatch (vuex.esm.js?2f62:512)
    at Store.boundDispatch [as dispatch] (vuex.esm.js?2f62:402)
    at VueComponent.emailLogin (Login.vue?7463:61)
    at click (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"19ca99f4-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/authentication/Login.vue?vue&type=template&id=566f7c02&scoped=true& (app.js:1022), <anonymous>:77:32)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at HTMLButtonElement.invoker (vue.runtime.esm.js?2b0e:2179)
    at HTMLButtonElement.original._wrapper (vue.runtime.esm.js?2b0e:6917)

The problem is here问题在这里

  login({ commit }, context, payload) {

you are passing just one argument but in the signature you put 3. Change it to您只传递了一个参数,但在您放置的签名中 3. 将其更改为

  login({ commit }, payload) {

Hope this helps!希望这可以帮助!

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

相关问题 TypeError:无法读取未定义的firebase和vuex的属性“ 0” - TypeError: Cannot read property '0' of undefined firebase and vuex firebase onAuth()抛出:TypeError无法读取未定义的属性'auth' - firebase onAuth() throwing: TypeError cannot read property 'auth' of undefined Vuex与Jest-无法读取属性 <getterName> 未定义 - Vuex with Jest - Cannot read property <getterName> of undefined 无法读取未定义的Axios,Vuex的属性“名称” - Cannot read property 'name' of undefined Axios, Vuex 商店 vuex 无法读取未定义的属性“商店” - Store vuex Cannot read property 'store' of undefined" 无法读取未定义的属性“auth” - Cannot read property 'auth' of undefined 无法读取未定义 VUEX 的属性“提交” - Cannot read property 'commit' of undefined VUEX 类型错误:调用“firebase.auth().onAuthStateChanged((user)”时无法读取未定义的属性“auth” - TypeError: Cannot read property 'auth' of undefined when calling " firebase.auth().onAuthStateChanged((user)" 类型错误:无法读取未定义的属性“凭据”-React-redux-firebase,Firebase 身份验证 - TypeError: Cannot read property 'credential' of undefined - React-redux-firebase, Firebase Auth TypeError:无法读取未定义Vuejs的属性'$ auth' - TypeError: Cannot read property '$auth' of undefined Vuejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM