简体   繁体   English

覆盖数据而不生成新的密钥/节点

[英]Overwrite data without generating new key/node

I'm trying to overwrite some data in my database.我试图覆盖我的数据库中的一些数据。 The structure is simple, being:结构很简单,就是:

recipes {
  user_1{
    recipe_1{data}
    recipe_2{data}
  }
  user_2{
    recipe_1{data}
    recipe_2{data}
  }
}

I'm trying to update an item and overwrite it with any changes using this, where recipe is the new object with the updated data I'm trying to save, and key is the unique key generated for each item in the database.我正在尝试更新一个项目并使用此recipe使用任何更改覆盖它,其中recipe是具有我尝试保存的更新数据的新对象, key是为数据库中的每个项目生成的唯一键。 In the database itself the labels recipe_1 and so on would be replaced with their unique keys.在数据库本身中,标签recipe_1等将被替换为它们的唯一键。 So this path should be just replacing the entire recipe_2 instead of adding a new node.所以这个路径应该只是替换整个recipe_2而不是添加一个新节点。

firebase.database().ref('recipes/' + userID + "/" + key).set({ recipe });

So say I was attempting to update user_1's recipe_2 with this, after going through, the database structure would then be:所以说我试图用这个更新user_1's recipe_2 ,经过之后,数据库结构将是:

data {
  user_1{
    recipe_1{data}
    recipe{
      recipe_2{data}
    }
  }
  user_2{
    recipe_1{data}
    recipe_2{data}
  }
}

How can I overwrite the item and keep the structure of the database the same while changing the contents of the recipe?如何在更改配方内容的同时覆盖项目并保持数据库结构不变?

You need to remove the extra {} from your set function which is creating a new node in the key value you are trying to update.您需要从set函数中删除额外的{} ,该函数会在您尝试更新的键值中创建一个新节点。

Change your code to将您的代码更改为
firebase.database().ref('recipes/' + userID + "/" + key).set(recipe);

暂无
暂无

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

相关问题 如何在不生成新密钥的情况下将数据推送到Firebase - How to push data to firebase without generating new key XML和PHP-如果节点存在,则覆盖数据,如果不存在,则创建一个新的 - XML and PHP - If node exists, overwrite the data, if not, create a new one 如何用节点 js 中的新数据覆盖我的 xlsx 工作表数据 - How can I overwrite my xlsx sheet data with new data in node js 如何通过本机反应将新字段数据更新到现有文档而不覆盖firebase-9中的现有数据 - How can update new Field data to existing document without overwrite to existing data in firebase-9 by react native 在不获取所有节点数据的情况下检查密钥是否存在 - Check if a key exists without getting all node data Firebase在不使用Javascript覆盖的情况下添加子数据 - Firebase Add Child Data without Overwrite in Javascript 循环多维数组以生成新数据并在节点 js/javascript 中附加新键值 - Loop over multidimensional array to generate new data and append new key values in node js / javascript 如何遍历新生成的数组而不覆盖它? - How can I loop through a new generated array without overwrite it? 为节点/ npm模块生成构建元数据 - Generating build meta data for a node/npm module 如何在不使用 node.js 覆盖现有 json 数据的情况下使用对象数组的键推送数据? - how to push a data with key of an array of objects without overwriting existing json data using node js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM