I am trying to make when someone logs in my site update or create statistics.
I have this code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src=".2.3/firebase-app.js"></script>
<script src=".2.3/firebase-firestore.js"></script>
<script src=".2.3/firebase-auth.js"></script>
</head>
<body>
<script>
var firebaseConfig = {
apiKey:
authDomain:
databaseURL:
projectId:
storageBucket:
messagingSenderId:
appId:
};
firebase.initializeApp(firebaseConfig)
var db = firebase.firestore();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
db.collection("users").set({
points: 0,
CurrentLevel: 0
})
}
});
console.log(user.points);
</script>
</body>
</html>
It should be working but when I try to run it, it says in the console db.collection(...).set is not a function
What Can I Do?
I am trying to make when someone logs in my site update or create statistics.
I have this code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://www.gstatic./firebasejs/7.2.3/firebase-app.js"></script>
<script src="https://www.gstatic./firebasejs/7.2.3/firebase-firestore.js"></script>
<script src="https://www.gstatic./firebasejs/7.2.3/firebase-auth.js"></script>
</head>
<body>
<script>
var firebaseConfig = {
apiKey:
authDomain:
databaseURL:
projectId:
storageBucket:
messagingSenderId:
appId:
};
firebase.initializeApp(firebaseConfig)
var db = firebase.firestore();
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
db.collection("users").set({
points: 0,
CurrentLevel: 0
})
}
});
console.log(user.points);
</script>
</body>
</html>
It should be working but when I try to run it, it says in the console db.collection(...).set is not a function
What Can I Do?
Share asked Nov 10, 2019 at 15:34 JosephJoseph 3652 gold badges5 silver badges11 bronze badges1 Answer
Reset to default 6db.collection("users") returns an object of CollectionReference. CollectionReference has no function 'set', Hence the error. Perhaps you are looking for the function 'add' which adds documents.
Replace
db.collection("users").set({
points: 0,
CurrentLevel: 0
})
with
db.collection("users").add({
points: 0,
CurrentLevel: 0
})
And I think that should solve your problem