简体   繁体   English

Vue.js firebase 实例 - 用户 UID 作为数据库参考

[英]Vue.js firebase instance - Users UID as database ref

Is it possible to get the following example working?是否可以使以下示例正常工作? I have pushed some objects into the firebase realtime-database and the reference is the UID of the signed in user.我已经将一些对象推送到 firebase 实时数据库中,参考是登录用户的 UID。 But even if I want to query the object at the reference which we can get from firebase.auth.currentUser.uid, I'm getting an error uid of NULL.但是,即使我想在我们可以从 firebase.auth.currentUser.uid 获得的参考文献中查询 object,我也会收到 Z6C3E226B4D4795D518ZAB341B0824EC2 的错误 uid。 Is there a way to get this functionality working?有没有办法让这个功能工作?

The Vue.js Code: Vue.js 代码:

import firebase from '.././firebase';
import { db } from '.././firebase';



export default {
    name: 'addMatch',
    data() {
        return{
            tableTennisData: [],
            player1Name: '',
            player2Name: '', 
            //Game 1
            firstMatchPlayer1Game1: '',
            firstMatchPlayer1Game2: '',
            firstMatchPlayer1Game3: '',
            firstMatchPlayer1Game4: '',
            firstMatchPlayer1Game5: '',
      }
    },

    firebase: {
        tableTennisData: db.ref(firebase.auth().currentUser.uid).child('tableTennis'), // UID Of Null error!
    },
        addMatch: function (e){
            e.preventDefault();
            const obj = {
            //Game 1
            player1Name: this.player1Name,
            player2Name: this.player2Name,
            firstMatchPlayer1Game1: this.firstMatchPlayer1Game1,
            firstMatchPlayer1Game2: this.firstMatchPlayer1Game2,
            firstMatchPlayer1Game3: this.firstMatchPlayer1Game3,
            firstMatchPlayer1Game4: this.firstMatchPlayer1Game4,
            firstMatchPlayer1Game5: this.firstMatchPlayer1Game5,
            }
            firebase.auth().onAuthStateChanged(function (){
                db.ref(firebase.auth().currentUser.uid).child('tableTennis').push(obj);
            });

            console.log(obj);
        },

    },

It sounds like the user isn't signed in yet.听起来用户尚未登录。

Keep in mind that it takes a moment for Firebase to restore the user's authentication state when you load a page.请记住,加载页面时 Firebase 需要一段时间才能恢复用户的身份验证 state。 If you call auth.currentUser before that, you will get back null .如果您在此之前调用auth.currentUser ,您将返回null

tableTennisData: db.ref(firebase.auth().currentUser.uid).child('tableTennis'), 

I don't see where you call this code, but you'll want to wrap it in an onAuthStateChanged handler as you already do in addMatch .我看不到您在哪里调用此代码,但您需要将其包装在onAuthStateChanged处理程序中,就像您在addMatch中所做的那样。

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

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