programing tip

Firebase 업데이트 대 세트

itbloger 2020. 12. 1. 07:44
반응형

Firebase 업데이트 대 세트


제목이 말하듯이, 나는 사이의 차이를 얻을 수 updateset. 또한 set을 사용하면 업데이트 예제가 똑같이 작동하므로 문서가 나를 도울 수 없습니다.

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

사용하는 동일한 예 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하고 set똑같은 일을.

감사합니다, Bene


원 자성

제공 한 두 샘플의 큰 차이점은 Firebase 서버로 보내는 쓰기 작업의 수입니다.

첫 번째 경우에는 하나의 update () 명령을 보냅니다. 전체 명령은 성공하거나 실패합니다. 예를 들어 사용자에게에 게시 할 수 /user-posts/' + uid있는 권한이 있지만에 게시 할 수있는 권한이없는 경우 /posts전체 작업이 실패합니다.

두 번째 경우에는 두 개의 개별 명령을 보냅니다. 동일한 권한을 사용하면 쓰기 /user-posts/' + uid는 성공하고 쓰기 /posts는 실패합니다.

부분 업데이트 대 전체 덮어 쓰기

이 예에서는 또 다른 차이점이 즉시 보이지 않습니다. 그러나 새 게시물을 작성하는 대신 기존 게시물의 제목과 본문을 업데이트한다고 가정 해보십시오.

이 코드를 사용하는 경우 :

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.

참고URL : https://stackoverflow.com/questions/38923644/firebase-update-vs-set

반응형