db.collection(...).set 不是函数

Jos*_*eph 4 javascript firebase google-cloud-firestore

我试图在有人登录我的网站时更新或创建统计信息。

我有这个代码

<!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.com/firebasejs/7.2.3/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/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>
Run Code Online (Sandbox Code Playgroud)

它应该可以工作,但是当我尝试运行它时,它在控制台中说 db.collection(...).set is not a function

我能做什么?

San*_*ana 6

db.collection("users") 返回CollectionReference的对象。CollectionReference 没有函数“设置”,因此出现错误。也许您正在寻找添加文档的功能“添加”。

代替

                db.collection("users").set({
                    points: 0,
                    CurrentLevel: 0
                })
Run Code Online (Sandbox Code Playgroud)

                db.collection("users").add({
                    points: 0,
                    CurrentLevel: 0
                })
Run Code Online (Sandbox Code Playgroud)

我认为这应该可以解决你的问题