【问题标题】:How can I dynamically specify the 'key' name on Firebase Database update() method?如何在 Firebase 数据库 update() 方法中动态指定“键”名称?
【发布时间】:2017-06-13 02:16:28
【问题描述】:

我正在使用 TypeScript 构建一个 Angular 4 应用程序,我想使用 update() 方法将一些数据添加到 Firebase 数据库中,同时从 userId 属性中动态指定“键”名称:

this.products = this.afdb.list('/products/');

this.products.update(this.productId, {
    this.userId : this.myRating
})

这里的问题是不能将this.userId 添加为“键”名称。谁能给我一个提示?

提前致谢。

【问题讨论】:

  • 也许:this.products.update(this.productId, (o={}, o[this.userId] = this.myRating, o))。但是,创建一个接受 this 参数并返回您的对象的函数会更好。

标签: javascript angular typescript firebase firebase-realtime-database


【解决方案1】:

您只需要使用不同的对象表示法...

{
    this.userId : this.myRating
}

不工作。

试试这个:

var obj = {};
obj[this.userId] = this.myRating;
this.products.update(this.productId, obj);

【讨论】:

  • 感谢您的回答!我想要达到的结果有点不同。我想用 'userId' 代替 'rating',所以结果会是这样的: ... '001: 5' .... 其中 '001' 是 userId 而 '5' 是评分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 2012-10-25
  • 2014-11-03
相关资源
最近更新 更多