Firebase更新与设置 [英] Firebase update vs set
问题描述
update
和 set
之间的区别。另外,文档不能帮助我,因为更新示例的工作原理完全相同,如果我使用set。 使用 亲切的问候, 您提供的两个示例之间的巨大差异在于它们发送到Firebase服务器的写入操作数量。 在第一种情况下,您发送一个update()命令。整个命令将成功或失败。例如:如果用户有权发布到 在第二种情况下,您发送两个单独的命令。使用相同的权限,写入 另外一个区别在这个例子中不是立即可见的。但是,假设您正在更新现有帖子的标题和正文,而不是撰写新帖子。 如果您使用以下代码: 您将会替换整个现有的文章。所以最初的 执行此代码后,原始的 As the title says, I can't get the differences between The The same example using So maybe the example from the docs should be updated, because now it looks like Kind regards,
Bene One big difference between the two samples you've given is in the number of write operations they send to the Firebase servers. 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 In the second case, you're sending two separate commands. With the same permissions, the write to 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: You'd be replacing the entire existing post. So the original If on the other hand you use an update: After executing this code, the original 这篇关于Firebase更新与设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ 函数writeNewPost(uid,username,title,body){
var postData = {
author:username,
uid:uid,
body:body,
title:title,
starCount:0
} ;
$ b var newPostKey = firebase.database()。ref()。child('posts')。push()。key;
var updates = {};
updates ['/ posts /'+ newPostKey] = postData;
updates ['/ user-posts /'+ uid +'/'+ newPostKey] = postData;
返回firebase.database()。ref()。update(updates);
$ / code> set $ c $
pre $ function $ write $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
author:username,
uid:uid,
body:body,
title:title,
starCount:0
};
$ b 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);
$ b因此,也许应该更新文档中的示例,因为现在看起来像 update
和 set
完成同样的事情。
Bene
原子性
/ user-posts /'+ uid
,但没有权限发布到 /帖子
,整个操作将失败。
/ user-posts /'+ uid
现在将成功,写入 / posts
将会失败。
部分更新与完全覆盖
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 新标题,身体:这是新的身体});
uid
, author
和 starCount
字段是没有了,只有新的标题
和 body
。
$ b $如果另一方面你使用更新:
$ p $ firebase.database()。ref()。child() '/ posts /'+ newPostKey)
.update({title:New title,body:This is the new body});
uid
,作者
和 starCount
仍然会在那里以及更新的标题
和 body
。update
and set
. Also the docs can't help me, as the update example works exactly the same if I use set instead.update
example from the docs: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);
}
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);
}
update
and set
do the exact same thing.Atomicity
/user-posts/' + uid
, but doesn't have permission to post to /posts
, the entire operation will fail./user-posts/' + uid
will now succeed, while the write to /posts
will fail.Partial update vs complete overwrite
firebase.database().ref().child('/posts/' + newPostKey)
.set({ title: "New title", body: "This is the new body" });
uid
, author
and starCount
fields would be gone and there'll just be the new title
and body
.firebase.database().ref().child('/posts/' + newPostKey)
.update({ title: "New title", body: "This is the new body" });
uid
, author
and starCount
will still be there as well as the updated title
and body
.