簡體   English   中英

db.collection(...).set 不是 function

[英]db.collection(…).set is not a function

當有人登錄我的網站時,我正在嘗試更新或創建統計信息。

我有這個代碼

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

我能做些什么?

db.collection("users") 返回CollectionReference的 object 。 CollectionReference 沒有 function 'set',因此出現錯誤。 也許您正在尋找添加文檔的 function 'add'。

代替

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

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

我認為這應該可以解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM