【发布时间】:2018-04-23 16:44:46
【问题描述】:
我正在尝试从特定用户加载数据并将该数据推送到特定输入字段。 Firebase 中的路径位于:
var query = db.ref('Clients/'+ clientName +'/form/');
我在mounted() 生命周期钩子中填充数据如下:
mounted(){
// Grab current user logged in from Firebase
var user = firebaseApp.auth().currentUser;
if ( user ){
// Grab displayName to use in path to retrieve that client's data
var clientName = firebaseApp.auth().currentUser.displayName;
// The path to that specific's client data
var query = db.ref('Clients/'+ clientName+'/form/');
query.once('value')
.then((snapshot) => {
// Get the client's location from Firebase and assign to clientLocation data in the DOM
this.clientLocation = snapshot.child('clientLocation').val();
});
}
}
当我进行更改并在不重新加载的情况下保存我的代码时,数据会被正确推送。但是在重新加载时出现以下错误:
Error in mounted hook: "TypeError: Cannot read property 'displayName' of null"
我玩过所有的生命周期钩子:
- 创建前
- 已创建
- 安装前
- 已安装
- 更新前
- 更新
- 销毁前
- 销毁
但数据不显示。似乎 firebaseApp 尚未“加载”,因此我无法获取“displayName”属性值,因此无法填充数据路径。
我应该如何/何时加载这个有效但似乎运行得太早的代码?
【问题讨论】:
标签: javascript firebase firebase-realtime-database vue.js vuejs2