Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
});
like image 302
Matthew Vanlandingham Avatar asked Sep 17 '25 10:09

Matthew Vanlandingham


1 Answers

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.

like image 190
Alex Avatar answered Sep 20 '25 00:09

Alex