簡體   English   中英

Vue.js firebase 實例 - 用戶 UID 作為數據庫參考

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

是否可以使以下示例正常工作? 我已經將一些對象推送到 firebase 實時數據庫中,參考是登錄用戶的 UID。 但是,即使我想在我們可以從 firebase.auth.currentUser.uid 獲得的參考文獻中查詢 object,我也會收到 Z6C3E226B4D4795D518ZAB341B0824EC2 的錯誤 uid。 有沒有辦法讓這個功能工作?

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);
        },

    },

聽起來用戶尚未登錄。

請記住,加載頁面時 Firebase 需要一段時間才能恢復用戶的身份驗證 state。 如果您在此之前調用auth.currentUser ,您將返回null

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

我看不到您在哪里調用此代碼,但您需要將其包裝在onAuthStateChanged處理程序中,就像您在addMatch中所做的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM