javascript - 按照 firebase 文档,.update 仍在覆盖

标签 javascript firebase firebase-realtime-database

我正在关注this guidethis reference document尝试使用键/值更新节点,其中键是在其他地方生成的pushID。我尝试了几种不同的方法,它总是会覆盖任何现有的 PushID 键,但它不会与同一父级上的其他非 PushID 键/值对混淆。关于我做错了什么有什么想法吗?我还应该注意到,这是在 firebase 云函数中运行的——这是代码

这是我尝试过的代码:

const parentNode = "zzABCDEFG" //this is an internal reference that I must use

const parentNodeRef = admin.database().ref().child(parentNode)

const PushIDSnap = await admin.database().ref('/ref/to/another/pushID/elsewhere').once('value')

const PushIDSnapVal = PushIDSnap.val() //getting the pushID that I want to set as the key

await parentNodeRef.child(PushIDSnapVal).set(Data)

我也尝试过:

const obj = {}

obj[PushIDSnapVal] = Data

console.log(obj) //console in firebase cloud-functions correctly shows { '-LH9-nBF6Wx3g6oSq154': '-LHBg3xKC51qYG6WAq65*FL' }

await parentNodeRef.update(obj) //this does the same thing--overwrites any existing AND DIFFERENT pushID's that are children of the parentNode

也尝试过,与上面类似,但使用完整路径 - 与其他两个相同

const obj = {}

const key = '/' + parentNode + '/' + PushIDSnapVal

obj[key] = Data

console.log(obj) //console in firebase cloud-functions correctly shows { '/ACGDIQCZZ/-LH9-nBF6Wx3g6oSq154': '-LHBg3xKC51qYG6WAq65*FL' }

await admin.database().ref().update(obj) //this does the same thing--overwrites any existing AND DIFFERENT pushID's that are children of the parentNode

这是我得到的 JSON 树(直接从 firebase 控制台中的导出文件复制):

"zzACGAFCG" : {

    "-LHA_9g4U8GzpjxiJmpa" : "-LHGXaRWxHhpWHeGk5Ob^MB", 

    "tT" : "D"

}, 

当我使用不同的pushID再次运行它时,它只会覆盖第一个键/值对,并且不会与“tT”子节点混淆

这就是我想要的:

"zzACGAFCG" : {

    "-LHA_9g4U8GzpjxiJmpa" : "-LHGXaRWxHhpWHeGk5Ob^MB", 

    "-LH9-nBF6Wx3g6oSq154" : "-LHFN0BZ2FNWUExOnulR^NA",

    "tT" : "D"

},

当我使用不同的 PushID 再次运行它时,它应该添加新的键/值对并保留旧的。

最佳答案

要从现有的变为所需的,您需要运行此更新:

var ref = firebase.database.ref("zzACGAFCG");
ref.update({ "-LH9-nBF6Wx3g6oSq154": "-LHFN0BZ2FNWUExOnulR^NA" });

这是尽可能少的,所以我假设您需要在代码中做更多的工作才能到达这里。我将在下面向您提供一些有关多位置更新如何工作的背景信息。

<小时/>

要认识到的最重要的事情是,更新语句会循环遍历您提供的对象中的所有键/路径,并且本质上对每个键/路径执行 set 操作。

假设您有以下 JSON:

{
  "root": {
    "messages": {
      "message1": "blablabla",
      "message2": "blablabla"
    }
  }
}

如果您运行此更新语句:

var root = firebase.database().ref("root");
root.update({ messages: { message3: "new message" } });

然后整个/root/messages节点将被更新替换,所以结果将是:

{
  "root": {
    "messages": {
      "message3": "new message"
    }
  }
}

正如我所说,除了更新对象中的第一级键/路径之外,数据库还会执行 set() 操作。它不进行深度合并。

如果要修补较低级别的节点,则需要确保更新对象的键中包含要更新的属性的完整路径。所以:

root.update({ "messages/message3": "new message" });

将导致:

{
  "root": {
    "messages": {
      "message1": "blablabla",
      "message2": "blablabla",
      "message3": "new message"
    }
  }
}

关于javascript - 按照 firebase 文档,.update 仍在覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327623/

相关文章:

javascript - 使用构造函数应用/调用

javascript - JQuery.animate() : HTML5 annimation with JQuery

firebase - 运行 Gradle 时出错 - gradlew.bat 异常退出(添加 firebase 后出错)

node.js - TypeError : rtdb. initStandalone 不是函数

firebase - Firebase 的云功能 - 在Write 上发送电子邮件

java - Firebase/GeoQuery - 最大半径 IF 语句

javascript - 在服务器端模板引擎上使用 React 或 Vue

javascript - 有条件申请一个!到 JavaScript 中的方法

ios - fcm onMessage 在模拟器上工作,但在物理设备上不起作用

ios - 无法让 Firebase 调用 observe/observeSingleEvent 回调