简体   繁体   中英

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. 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. Is there a way to get this functionality working?

The Vue.js Code:

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. If you call auth.currentUser before that, you will get back 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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