本文介绍了Firebase更新与设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我无法得到 update 和 set 之间的区别。另外,文档不能帮助我,因为更新示例的工作原理完全相同,如果我使用set。



update
$ 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
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

解决方案

原子性



您提供的两个示例之间的巨大差异在于它们发送到Firebase服务器的写入操作数量。



在第一种情况下,您发送一个update()命令。整个命令将成功或失败。例如:如果用户有权发布到 / 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 。


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.

The 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);
}

The same example using 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.

Kind regards,Bene

解决方案

Atomicity

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 /user-posts/' + uid, but doesn't have permission to post to /posts, the entire operation will fail.

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.

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.

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.

这篇关于Firebase更新与设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:12