使用 navParams 的 Angularfire2 查询不起作用
Angularfire2 query using navParams not working
我想使用 navParams
获取一些数据,但它不起作用,但如果我提供静态值,它就可以工作。我不知道出了什么问题。这是我的代码,它不起作用并且 return 为空
console.log(this.navParams.get('id')) // prints 10
const id = this.navParams.get('id');
this.categoryRef = this.db.list('/category', ref => ref.orderByChild('id').equalTo(id));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
如果我给出静态值,那么它的工作方式就像
const id = 10;
this.categoryRef = this.db.list('/category', ref => ref.orderByChild('id').equalTo(id));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
你可以像下面这样试试吗?
myId:number;//declare a global variable
constructor(){
this.myId=this.navParams.get('id');
this.categoryRef = this.db.list('/category', ref =>
ref.orderByChild('id').equalTo(this.myId));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
}
实际上我在 firebase 数据库方面发现了一个问题。我的 id 没有像 "1"
这样用双引号存储它被保存为 1
然后我把它变成双引号然后它工作正常
我想使用 navParams
获取一些数据,但它不起作用,但如果我提供静态值,它就可以工作。我不知道出了什么问题。这是我的代码,它不起作用并且 return 为空
console.log(this.navParams.get('id')) // prints 10
const id = this.navParams.get('id');
this.categoryRef = this.db.list('/category', ref => ref.orderByChild('id').equalTo(id));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
如果我给出静态值,那么它的工作方式就像
const id = 10;
this.categoryRef = this.db.list('/category', ref => ref.orderByChild('id').equalTo(id));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
你可以像下面这样试试吗?
myId:number;//declare a global variable
constructor(){
this.myId=this.navParams.get('id');
this.categoryRef = this.db.list('/category', ref =>
ref.orderByChild('id').equalTo(this.myId));
this.category = this.categoryRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
}
实际上我在 firebase 数据库方面发现了一个问题。我的 id 没有像 "1"
这样用双引号存储它被保存为 1
然后我把它变成双引号然后它工作正常