【发布时间】:2020-08-24 05:31:31
【问题描述】:
是否可以使以下示例正常工作?我已将一些对象推送到 firebase 实时数据库中,参考是登录用户的 UID。但是,即使我想在我们可以从 firebase.auth.currentUser.uid 获得的引用处查询对象,我也会得到一个 NULL 的错误 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);
},
},
【问题讨论】:
-
"I'm getting an error uid of NULL" 您收到的确切错误消息是什么,发生在哪一行?
-
@FrankvanPuffelen 感谢您的回答 :) 通过将 firebase 数据库对象分配给“tableTennisData”数组,我遇到了错误。在特定行有注释。错误消息:“addMatch.vue?c217:231 Uncaught TypeError: Cannot read property 'uid' of null”
-
@FrankvanPuffelen 如何将 Firebase 对象从 Vue 包装到 onAuthStateChange 处理程序中?由于 Vue.js 语法,这很困难......
标签: javascript firebase vue.js firebase-realtime-database vuefire