Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试访问vuex中的数据时,Firebase SnapShot.val()返回null_Javascript_Firebase_Vue.js_Firebase Realtime Database_Vuex - Fatal编程技术网

Javascript 尝试访问vuex中的数据时,Firebase SnapShot.val()返回null

Javascript 尝试访问vuex中的数据时,Firebase SnapShot.val()返回null,javascript,firebase,vue.js,firebase-realtime-database,vuex,Javascript,Firebase,Vue.js,Firebase Realtime Database,Vuex,我正在创建一个学生、教职员工和非教职员工都可以访问的应用程序 我的表单数据如下所示: formData: { name: "", email: "", password: "", select: null }, options: ["Student", "Staff", "Non-Teaching Staff"], 当然,在Vuex商店中,我可以向以下用户注册: registerUsers({}, payload) { f

我正在创建一个学生、教职员工和非教职员工都可以访问的应用程序

我的表单数据如下所示:

formData: {
      name: "",
      email: "",
      password: "",
      select: null
    },
    options: ["Student", "Staff", "Non-Teaching Staff"],
当然,在Vuex商店中,我可以向以下用户注册:

registerUsers({}, payload) {
    firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
      .then(res => {
        const userId = firebaseAuth.currentUser.uid;
        console.log(res)
        Notify.create({
          message: 'Regsitration Successful!.',
          color: 'primary',
          classes: 'quick'
        })
        //set student
        firebaseDb.ref(`users/'${userId}`).set({
          name: payload.name,
          email: payload.email,
          select: payload.select
        });
      })
      .catch(err => {
        console.log(err)
        Notify.create({
          message: `${err.message}`,
          classes: 'quick',
          color: 'negative'
        })
      })
我还可以通过以下方式登录用户:

loginUsers({}, payload) {
    firebaseAuth.signInWithEmailAndPassword(payload.email, payload.password)
      .then(res => {
        console.log(res);
        Notify.create({
          message: 'Success!',
          classes: 'quick',
          color: 'positive'
        })
      })
      .catch(err => {
        console.log();
        Notify.create({
          message: `${err.message}`,
          classes: 'quick',
          color: 'negative'
        })
      })
  },
问题来自以下方面:

handleAuthStateChange() {
    firebaseAuth.onAuthStateChanged(user => {
      if (user) {
        //set Student
        const studentId = firebaseAuth.currentUser.uid;
        console.log(studentId)
        firebaseDb.ref(`users/${studentId}`).once('value', snapshot => {
          console.log(snapshot.val())
        })
      }
    })
  },
控制台中的Snapshot.val()返回null


我写错了。

看来,通过调用
firebaseDb.ref(`users/'${userId}').set({…})
您正在节点下创建用户

users/'userId
使用单引号(

然后尝试读取节点

users/userId
如果您错误地添加了单个报价的假设是正确的,则不存在


另外,请注意,您不需要这样做

firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
  .then(res => {
    const userId = firebaseAuth.currentUser.uid;
    //...
因为
createUserWithEmailAndPassword()
返回一个。因此,您可以:

firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
  .then(res => {
    const userId = res.user.uid;
    //...
handleAuthStateChange() {
    firebaseAuth.onAuthStateChanged(user => {
      if (user) {
        const studentId = user.uid;
        //......
你也可以做到:

firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
  .then(res => {
    const userId = res.user.uid;
    //...
handleAuthStateChange() {
    firebaseAuth.onAuthStateChanged(user => {
      if (user) {
        const studentId = user.uid;
        //......

单报价是问题所在,非常感谢