最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - db.collection(...).set is not a function - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

db.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

发布评论

评论列表(0)

  1. 暂无评论