简体   繁体   English

我可以修改本地 javascript 对象吗?

[英]Can I modify a local javascript object?

I have a stats object which is returned from firebase database我有一个从 firebase 数据库返回的 stats 对象

    var stats = await firebase
      .database()
      .ref("statistics")
      .child(currentUser.uid)
      .once("value");

The format of the object is like below:对象的格式如下:

stats  Object {
  "all": Object {
    "pending": 1,
    "settled": 0,
    "vol": 100,
  },
  "mtd": Object {
    "pending": 1,
    "settled": 0,
    "vol": 100,
  },
  "qtd": Object {
    "pending": 1,
    "settled": 0,
    "vol": 100,
  },
  "today": Object {
    "date": "Dec 7, 2019",
    "pending": 1,
    "settled": 0,
    "vol": 100,
  },
  "yesterday": Object {
    "date": "Dec 6, 2019",
    "pending": 0,
    "settled": 0,
    "vol": 0,
  },
  "ytd": Object {
    "pending": 1,
    "settled": 0,
    "vol": 100,
  },
}

Can I directly overwrite the stats.val().yesterday, say set the stats.val().yesterday to我可以直接覆盖 stats.val().yesterday,比如说将 stats.val().yesterday 设置为

    "date": "Dec 6, 2020",
    "pending": 1,
    "settled": 10,
    "vol": 1000,
  }

then use stats.val() with the latest changes?然后使用 stats.val() 进行最新更改? From what I did I still see old data before making changes to yesterday field.根据我所做的,在对yesterday字段进行更改之前,我仍然看到旧数据。 In this case, how can I get stats.val() with changes?在这种情况下,如何获得 stats.val() 的变化? Thanks!谢谢!

val() returns a copy of the data in the snapshot, not the actual underlying object. val()返回快照中数据的副本,而不是实际的底层对象。 If you modify this copy, it won't change what's stored in the snapshot.如果您修改此副本,它不会更改存储在快照中的内容。 In this way, DataSnapshot is said to be "immutable" - it can't be changed.通过这种方式,DataSnapshot 被称为“不可变的”——它不能被改变。 So, what you're trying to do is not possible.所以,你想要做的事情是不可能的。

Typically, app code is supposed to call val() just once to get a copy of the data, then use that copy to populate the UI.通常,应用程序代码应该只调用一次val()来获取数据的副本,然后使用该副本来填充 UI。

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

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