Firebase:如果文档不存在,更新Firestore失败

时间:2017-11-23 00:23:39

标签: firebase google-cloud-firestore

使用Firestore时,如果我正在写一个新用户(uid),则下面会失败并给我一个粗略的错误。如果我将.set用于不存在的文档,则它可以正常工作。 Firebase Realtime DB允许我使用.update创建新记录。

我的问题是:这是Firestore的预期行为还是我的脚本不好?

firebase.auth().signInWithPopup(provider).then(function(result) {
    var thisUid = firebase.auth().currentUser.uid;
    var docRef = firebase.firestore().collection("users").doc(thisUid);
    var o = {};
    docRef.get().then(function(thisDoc) {
        if (thisDoc.exists) {
            //user is already there, write only last login
            o.lastLoginDate = Date.now();
            docRef.update(o);
        } else {
            //new user
            o.displayName = firebase.auth().currentUser.displayName;
            o.accountCreatedDate = Date.now();
            o.lastLoginDate = Date.now();
            // Send it
            docRef.update(o);
        }
    });
}).catch(function(error) {
    toast(error.message);
});

我得到的错误是:

error.js:149 Uncaught (in promise) Error: no entity to update: app: "s~myapp-abcdefg"<br/>path <<br/>  Element {<br/>    type: "users"<br/>    name: "WlM8mo-my-long-uid-h4pu2"<br/>  }<br/>><br/>
    at new e (error.js:149)
    at webchannel_connection.js:241
    at X.<anonymous> (webchannel_connection.js:185)
    at Cb (index.js:22)
    at X.g.dispatchEvent (index.js:20)
    at Ce.Ea (index.js:94)
    at ke.g.Na (index.js:82)
    at Tc (index.js:43)
    at Sc (index.js:40)
    at L.g.Ra (index.js:38)

0 个答案:

没有答案