【问题标题】:Angularfire2 query using navParams not working使用 navParams 的 Angularfire2 查询不起作用
【发布时间】:2017-11-11 16:46:10
【问题描述】:

我想使用navParams 获取一些数据,但它不工作,但如果我提供静态值,那么它工作。我不知道出了什么问题。这是我的代码不起作用并返回空

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

【问题讨论】:

  • 你在哪里调用这个方法?生命周期事件?
  • 我在ionViewDidLoad()constructor都试过了

标签: firebase firebase-realtime-database ionic3 angularfire2


【解决方案1】:

你能像下面这样试试吗?

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

【讨论】:

  • 你可以调试没有? this.myId 的值是多少?
  • console.log(this.myId); 打印 10
  • 那有什么问题呢?
  • 这很奇怪。如果我使用参数,则结果为空。
  • 这里没有版本问题。你在来自params 时告诉this.myId' has 10` 那么这就是你不需要的吗?
【解决方案2】:

实际上我在 firebase 数据库方面发现了一个问题。我的 id 没有用像"1" 这样的双引号存储,它被保存为1 然后我把它变成双引号,然后它工作正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2018-06-14
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多