简体   繁体   English

使用navParams的Angularfire2查询不起作用

[英]Angularfire2 query using navParams not working

I want to get some data using navParams but it is not working but if I provide static value then it's work. 我想使用navParams获取一些数据,但是它不起作用,但是如果我提供静态值,那就可以了。 I don't know what is wrong. 我不知道怎么了 Here is my code that does not work and return empty 这是我的代码不起作用,返回空

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() }));
});

if I give static value then it works like 如果我给静态值,那么它就像

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() }));
});

Can you try like below? 你可以像下面这样尝试吗?

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() }));
});
}

Actually I found an issue on firebase database side. 实际上,我在Firebase数据库方面发现了一个问题。 My id was not stored with double quotes like "1" it was saved as 1 then I make it to double quotes and it works fine then 我的ID没有用双引号(如"1" )存储,它被另存为1然后我将其用双引号括起来,然后工作正常 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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