Javascript Firebase 更新与设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38923644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase update vs set
提问by Bene
As the title says, I can't get the differences between update
and set
. Also the docs can't help me, as the update example works exactly the same if I use set instead.
正如标题所说,我不能得到之间的区别update
和set
。文档也帮不了我,因为如果我使用 set ,更新示例的工作方式完全相同。
The update
example from the docs:
update
文档中的示例:
function writeNewPost(uid, username, title, body) {
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0
};
var newPostKey = firebase.database().ref().child('posts').push().key;
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
The same example using set
同样的例子使用 set
function writeNewPost(uid, username, title, body) {
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0
};
var newPostKey = firebase.database().ref().child('posts').push().key;
firebase.database().ref().child('/posts/' + newPostKey).set(postData);
firebase.database().ref().child('/user-posts/' + uid + '/' + newPostKey).set(postData);
}
So maybe the example from the docs should be updated, because now it looks like update
and set
do the exact same thing.
所以也许文档中的例子应该更新,因为现在它看起来update
和set
做完全一样的事情。
Kind regards, Bene
亲切的问候,贝内
回答by Frank van Puffelen
Atomicity
原子性
One big difference between the two samples you've given is in the number of write operations they send to the Firebase servers.
您提供的两个示例之间的一大区别在于它们发送到 Firebase 服务器的写入操作数量。
In the first case, you're sending a single update() command. That entire command will either succeed or fail. For example: if the user has permission to post to /user-posts/' + uid
, but doesn't have permission to post to /posts
, the entire operation will fail.
在第一种情况下,您发送的是单个 update() 命令。整个命令要么成功要么失败。例如:如果用户有发帖权限/user-posts/' + uid
,但没有发帖权限/posts
,整个操作就会失败。
In the second case, you're sending two separate commands. With the same permissions, the write to /user-posts/' + uid
will now succeed, while the write to /posts
will fail.
在第二种情况下,您发送两个单独的命令。使用相同的权限,/user-posts/' + uid
现在写入将成功,而写入/posts
将失败。
Partial update vs complete overwrite
部分更新与完全覆盖
Another difference is not immediately visible in this example. But say that you're updating the title and body of an existing post, instead of writing a new post.
在这个例子中,另一个区别不是立即可见的。但是假设您要更新现有帖子的标题和正文,而不是撰写新帖子。
If you'd use this code:
如果您使用此代码:
firebase.database().ref().child('/posts/' + newPostKey)
.set({ title: "New title", body: "This is the new body" });
You'd be replacing the entire existing post. So the original uid
, author
and starCount
fields would be gone and there'll just be the new title
and body
.
您将替换整个现有帖子。所以原来的uid
, author
andstarCount
字段将消失,只剩下新的title
and body
。
If on the other hand you use an update:
另一方面,如果您使用更新:
firebase.database().ref().child('/posts/' + newPostKey)
.update({ title: "New title", body: "This is the new body" });
After executing this code, the original uid
, author
and starCount
will still be there as well as the updated title
and body
.
执行此代码后,原始uid
,author
和starCount
将仍然存在以及更新的title
和body
。