我用过
fAuth.authState.subscribe( user => {
if (user) { this.userId = user.uid }
});
但是,我现在尝试通过id在userProfile中搜索以获取点值。
getTotalPoints(){
this.db.list('/userProfile/' + this.userId).valueChanges().subscribe(
data => {
console.log(JSON.stringify(data))
this.pointsValue = data;
}
)
}
我收到错误
运行时错误
未捕获(承诺):错误:Reference.child失败:第一个参数是无效路径=" / userProfile / [object Object]"。路径必须是非空字符串,并且不能包含"。","#"," $"," [& #34;,或"]"错误:Reference.child失败:第一个参数是无效路径=" / userProfile / [object Object]"。路径必须是非空字符串,并且不能包含"。","#"," $"," [& #34;,或"]"在Object.exports.validatePathString
很抱歉,如果答案很简单,那就很新了。
这里是 DB
整页代码
import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic- angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import 'rxjs/add/operator/map';
@IonicPage()
@Component({
selector: 'page-dashboard',
templateUrl: 'dashboard.html',
})
export class DashboardPage {
pointsValue: {}
userId = {}
tasks:Array<any> = [];
constructor(public navCtrl: NavController, public navParams: NavParams, public db: AngularFireDatabase, public afAuth: AngularFireAuth, public loadingCtrl: LoadingController) {
afAuth.authState.subscribe( user => {
if (user) { this.userId = user.uid }
});
this.getTotalPoints()
this.tasks = [
{
name: 'Clean bedroom',
status: '100'
},
{
name: 'Empty dishwasher',
status: '100'
},
{
name: 'Take dog for a walk',
status: '100'
},
];
}
checkUser() {
console.log(this.userId);
}
getTotalPoints(){
this.db.list('/userProfile/' + this.userId).valueChanges().subscribe(
data => {
console.log(JSON.stringify(data))
this.pointsValue = data;
}
)
}
}
答案 0 :(得分:1)
基于错误
第一个参数是无效路径=“/ userProfile / [object Object]”
userId = {} //is an object, where it should be a non-object data type
并在您的代码中
//You're doing a async method
afAuth.authState.subscribe( user => {
if (user) { this.userId = user.uid }
});
/*After that you called your method, which caused userId
to be an object since you initialized it as an empty object */
this.getTotalPoints()
为了更好的实现,请这样做
//Set userId as null
userId: any = null
使此函数具有userId
参数 getTotalPoints(id: any){
this.db.list('/userProfile/' + id).valueChanges().subscribe(
data => {
console.log(JSON.stringify(data))
this.pointsValue = data;
}
)
}
然后在订阅调用中这样做
afAuth.authState.subscribe( user => {
if (user) {
this.userId = user.uid
this.getTotalPoints(this.userId);
}
});
答案 1 :(得分:0)
如果要获取Firebase对象的数据
,则使用对象而不是列表getTotalPoints(){
this.db.object('/userProfile/' + this.userId).valueChanges().subscribe(
data => {
console.log(JSON.stringify(data))
this.pointsValue = data;
}
)
AngularFire2 doc中的更多信息:https://github.com/angular/angularfire2/blob/master/docs/rtdb/objects.md