Firebase Firestore 错误 400:WebChannel 传输错误

问题描述 投票:0回答:1

我正在尝试在我的 Web 应用程序中设置 Firebase 身份验证和 Firestore。用户身份验证工作正常,但当我尝试将文档添加到 Firestore 时,遇到 400 错误,并显示消息 Connection WebChannel Transport errored

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Firebase Test</title>
    <script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.0.0/firebase-firestore.js"></script>
</head>
<body>

    <script>
        // Firebase configuration
        const firebaseConfig = {
            apiKey: "YOUR_API_KEY",
            authDomain: "YOUR_AUTH_DOMAIN",
            projectId: "YOUR_PROJECT_ID",
            storageBucket: "YOUR_STORAGE_BUCKET",
            messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
            appId: "YOUR_APP_ID",
            measurementId: "YOUR_MEASUREMENT_ID"
        };
        
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);

        // Authentication state observer
        firebase.auth().onAuthStateChanged(function(user) {
            if (!user) {
                console.log("No user is signed in.");
                window.location.href = '/login';
            } else {
                console.log("User is signed in:", user);
                
                // Firestore reference
                const db = firebase.firestore();

                // Add a document to Firestore
                db.collection("test").add({
                    name: "Test",
                    time: new Date()
                })
                .then((docRef) => {
                    console.log("Document written with ID:", docRef.id);
                })
                .catch((error) => {
                    console.error("Error adding document:", error);
                });
            }
        });
    </script>
</body>
</html>

在浏览器控制台中,我看到以下错误消息:

User is signed in
firestore.googleapis.com/google.firestore.v1.Firestore/Write/channel?database=projects%2Ftask-aces%2Fdatabases%2F(default)&gsessionid=DtgfmiAfriWSngJvBdKhoCyS1jS1A2ZM_tw0QEdp13s&VER=8&RID=rpc&SID=WOLNzv6Mk5zewit_TB4L4w&CI=0&AID=0&TYPE=xmlhttp&zx=83fwhscdlzxy&t=1:1
Failed to load resource: the server responded with a status of 400 ()
logger.ts:115 [2024-07-24T13:13:01.811Z] @firebase/firestore: Firestore (8.0.0): Connection WebChannel transport errored: pr

我已检查我的 Firestore 规则并确保它们允许经过身份验证的用户进行读写访问。以下是我当前的 Firestore 规则:

service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read, write: if request.auth != null;
        }
    }
}

我还验证了我的 Firebase 配置参数是否正确。可能是什么原因导致此问题?我该如何解决?

附加信息:

  1. 我使用的是 Firebase SDK 版本 8.0.0。
  2. 用户认证正常,onAuthStateChanged回调按预期触发。

任何帮助或指导将不胜感激!

firebase google-cloud-firestore
1个回答
0
投票

我自己发现了这个问题。

我的问题是我将 firestore 数据库的名称从

(default)
更改为其他名称。我现在已将数据库名称恢复为
(default)
,并且不再遇到此问题。

© www.soinside.com 2019 - 2024. All rights reserved.