'How to fix "Uncaught TypeError: db.collection is not a function"?
code in which the error occurs:
const countRef = db.collection('counter').doc('counter');
const increment = firebase.Firestore.FieldValue.increment(1);
const decrement = firebase.Firestore.FieldValue.increment(-1);
function change(num) {
if (num>0) {
countRef.update({ counter: increment });
} else if (num<0) {
countRef.update({ counter: decrement });
};
};
The error is in the first line of code. I have also tried to search the internet for answers, but I have not yet found any.
Solution 1:[1]
As you haven't shown what db is defined to, this answer may be incorrect/not help.
The issue seems to be that you are setting db to an object which doesn't have collection().
Make sure that the db variable is defined and set to the following:
const db = firebase.firestore();
This object has the collection() method defined in it, which may fix the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arnav Thorat |
