Firebase: Update Firestore Fails If Document Does Not Exist

Viewed 5886

With Firestore, the below fails and gives me a gnarly error if I'm writing a new user(uid). It works if I use .set for a non-existent document. Firebase Realtime DB allows me to use .update to create a new record.

My question is: is this expected behavior for Firestore or is my script bad?

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

The error I get is:

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 Answers
Related