Search code examples
javascriptgoogle-cloud-functionsnangoogle-cloud-firestore

Cloud function is writing NaN to firestore


I created a cloud Function to update a following count every time there is a new follower. However, for whatever reason, NaN gets written to the document instead of an incremented count. What is NaN and why is it getting written instead of the appropriate number? This is my code:

var transaction = db.runTransaction(t => {
    return t.get(countRef)
        .then(doc => {
            var new_count = doc.data.following_count + 1;
            t.update(countRef, { following_count: new_count });
        });
}).then(result => {
    console.log('Transaction success!');
})
.catch(err => {
    console.log('Transaction failure:', err);
});

Solution

  • NaN is Not a Number. As some commenters said (but without actually posting an answer), this is caused by operations on non-numeric input, such as undefined values or strings.