【问题标题】:db.collection(...).set is not a functiondb.collection(...).set 不是函数
【发布时间】:2019-11-10 15:34:26
【问题描述】:

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

我有这个代码

<!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>

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

我能做什么?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

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

    替换

                    db.collection("users").set({
                        points: 0,
                        CurrentLevel: 0
                    })
    

                    db.collection("users").add({
                        points: 0,
                        CurrentLevel: 0
                    })
    

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

    【讨论】:

    • 您也可以使用.collection(...).doc().set(...)(这样可以更轻松地找出创建和更新模式之间的共性。
    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2018-06-04
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2021-10-31
    相关资源
    最近更新 更多