是否可以重命名Firebase实时数据库中的某个键? [英] Is it possible to rename a key in the Firebase Realtime Database?
问题描述
让我们使用下面的数据:
我使用set()来写入数据。 移动不是一个选项,因为它会擦除 那么,有没有办法更新键值? 更新:这就是我目前用来更新 我想我明白你想要做什么。 Firebase没有通过更新重命名部分路径的概念。相反,您必须完全删除现有的节点并重新创建它。你可以这样做: 这将从 警告:这依赖于读取数据,然后执行第二个异步更新。如果您可能有多个用户同时操作相同的数据,则可能会导致问题。您可以使用交易以原子方式执行此操作,但如果 一次编辑数据,上面的解决方案是好的。如果没有,您可能需要考虑使用非用户控制的标识符,例如推送标识。 I was wondering, is there a way to update the key value? Let´s use the following data: I am using set() to write the data.
Now, I want the user to edit their Moving is not an option because it will erase the So, is there a way to update the key value?
or, is there a better approach to this? I accept suggestions. Thank you! Update: This is what I´m currently using to update the book title inside
I think I see what you're trying to do. Firebase doesn't have the concept of "renaming" a part of the path via update. Instead you will have to completely remove the existing node and recreate it. You can do that like so: This will remove the info from Caveat: This relies on reading the data and then performing a second async update. If you are likely to have multiple users operating on the same data at the same time this could cause issues. You could use a transaction to do this atomically but if If one person is likely to edit the data at a time, the above solution is fine. If not, you may want to consider using a non-user-controlled identifier such as a push id. 这篇关于是否可以重命名Firebase实时数据库中的某个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
现在,我希望用户编辑它们的 bookTitle
,它需要在两个地方都改变。我尝试使用更新(),但我似乎无法使其工作。我只能编辑 bookInfo
不在 books 中的
bookTitle
。
bookData
。
我也尝试使用push(),但是我不能正确搜索,因为我没有pushID(我需要搜索,因为用户不能有两本相同的名称)
或者,有没有更好的方法呢?我接受建议。谢谢!
bookInfo < code $>
$ $ p $ var bookName = document.getElementById('bookName').value;
$ b $ firebase.database()。ref('books /'+ bookName +'/ bookInfo').update({
bookTitle:bookName
));
var booksRef = firebase.database()。ref('books');
booksRef.child(oldTitle).once('value')。then(function(snap){
var data = snap.val();
data.bookInfo.bookTitle = newTitle;
var update = {};
update [oldTitle] = null;
update [newTitle] = data;
return booksRef.update(update);
}) ;
books / oldTitle
并在 books / newTitle
中重新填充新标题。
/ books
是一个包含许多节点的顶级资源,可能会导致性能问题。
bookTitle
and it needs to change on both places. I tried using update() but I can´t seem to make it work. I can only edit the bookTitle
in bookInfo
NOT on books
.bookData
.
I also tried writing using push() but then, I can´t search properly because I don´t have the pushID (I need the search because users can't have two books with the same name)bookInfo
var bookName = document.getElementById('bookName').value;
firebase.database().ref('books/' + bookName + '/bookInfo').update({
bookTitle : bookName
});
var booksRef = firebase.database().ref('books');
booksRef.child(oldTitle).once('value').then(function(snap) {
var data = snap.val();
data.bookInfo.bookTitle = newTitle;
var update = {};
update[oldTitle] = null;
update[newTitle] = data;
return booksRef.update(update);
});
books/oldTitle
and re-populate it with a new title in books/newTitle
./books
is a top-level resource with many nodes that may cause performance problems.