【问题标题】:Angular2 get single data from firebase and card schemaAngular2从firebase和card schema中获取单个数据
【发布时间】:2018-03-20 03:04:21
【问题描述】:

大家好,我是 firebase 和电子商务的新手,我只是想用虚拟应用程序来列出电子商务产品。但是,我无法提出卡片的想法。然后在 firebase 制作了一张新表作为卡片和输入userId,以便我根据userId在购买页面获取数据但无法获取单个数据。(能够使用this.card = this.afdatabase.list('/card/') )获取所有列表。

这是我的架构,请建议我获得更好的架构。

我想我找到了像 list('/card/').foreach(d=>) 这样的解决方案 但是检查所有数据会非常耗时。 根据userId拿货的解决方法是什么?

编辑我的 addCard 并获取数据

card: FirebaseListObservable<any[]>;
addCard(item, amount) {
this.card.push(
  {
    productDetails: item,
    amount: amount,
    userId: this.userId
  }
);


this.card = this.afdatabase.list('/card/');

当我 console.log 时返回

 this.card= this.afdatabase.list('/card', {
  query: {
    orderByChild: 'userId',
    equalTo: this.userId
  }
});

【问题讨论】:

  • 如果你也能展示你的服务会很有帮助
  • 对不起,我做到了。如果您需要更多信息,请告诉我。谢谢

标签: angular firebase-realtime-database angularfire2


【解决方案1】:

如果你想通过userid获取数据,你可以这样做

this.card = this.afdatabase.list('/card', { query : {
        orderByChild: 'userid',
        equalTo: this.userid
      });

【讨论】:

  • 感谢您的快速回复,但我尝试了您的代码和控制台记录 this.card 并返回 firebaseListObservable 我将把图像放在帖子中。我怎样才能在那里获取数据。谢谢
  • 你好,我刚刚用 orderbychild 和 foreach 做的,是正确的方法还是有更好的主意? console.log("card ", this.card.forEach(d=>{ console.log("aaaa: ", d); }))
  • 我实际上看不出您需要 foreach 的原因。仅仅使用 orderbychild 还不够吗?
  • 但我在帖子中以图片形式给出了结果,你看到了吗,我无法在没有 foreach 的情况下获取数据。
  • 你能做一个this.card的控制台日志吗?你是怎么展示的?你订阅了它还是在你的模板中使用了异步管道?
猜你喜欢
  • 2018-04-21
  • 2016-11-10
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多