firebase Firestore:GrpcConnection RPC“写入”流0x48b21b62错误,代码:13留言内容:13内部:收到RST_STREAM,代码为% 2

z4bn682m  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(220)

当我尝试插入一个文档时,我在Firebase模拟器中得到了这个错误。我是Firebase的新手,所以可能我在做一些愚蠢的事情,但我不能弄清楚。

**注意:**我从web版本9的文档中获取了代码示例,因为node.js的代码告诉我db中没有collection函数。所以我遵循了this thread中的建议

以下是我的Firebase依赖项:

zied@zied-N752VX:~/Work/WS/linkedinCv-back$ pnpm view firebase dependencies
{
  '@firebase/app': '0.9.7',
  '@firebase/app-compat': '0.2.7',
  '@firebase/app-types': '0.9.0',
  '@firebase/auth': '0.22.0',
  '@firebase/auth-compat': '0.3.7',
  '@firebase/database': '0.14.4',
  '@firebase/database-compat': '0.3.4',
  '@firebase/firestore': '3.10.0',
  '@firebase/firestore-compat': '0.3.6',
  '@firebase/functions': '0.9.4',
  '@firebase/functions-compat': '0.3.4',
  '@firebase/installations': '0.6.4',
  '@firebase/installations-compat': '0.2.4',
  '@firebase/messaging': '0.12.4',
  '@firebase/messaging-compat': '0.2.4',
  '@firebase/storage': '0.11.2',
  '@firebase/storage-compat': '0.3.2',
  '@firebase/performance': '0.6.4',
  '@firebase/performance-compat': '0.2.4',
  '@firebase/remote-config': '0.4.4',
  '@firebase/remote-config-compat': '0.2.4',
  '@firebase/analytics': '0.9.5',
  '@firebase/analytics-compat': '0.2.5',
  '@firebase/app-check': '0.6.4',
  '@firebase/app-check-compat': '0.3.4',
  '@firebase/util': '1.9.3'
}

我的初始化代码如下

import { initializeApp } from "firebase/app";
// import { getFirestore, connectFirestoreEmulator } from 'firebase-admin/firestore';

import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";

// firebaseApps previously initialized using initializeApp()
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "XXXXX",
    authDomain: "linkedincv-bdbec.firebaseapp.com",
    projectId: "linkedincv-bdbec",
    storageBucket: "linkedincv-bdbec.appspot.com",
    messagingSenderId: "472466845815",
    appId: "XXXXX",
    measurementId: "XXXXX",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
// const db = getFirestore(app);

const db = getFirestore(app);
connectFirestoreEmulator(db, 'localhost', 4000);

export {db}

触发错误的代码如下:

import {db} from '../../../lib/middleware/firebaseInit'
import { doc, setDoc } from "firebase/firestore";

export async function POST({request}) {
    try {
        const payload = await request.json();
        const uniqueUrl = payload.uniqueUrl;

        // Store the 'uniqueUrl' field in Firestore
        const docRef = await setDoc(doc(db, "json_cv", "testId"), payload)
        // Return the document ID of the stored object
        return {
            body: JSON.stringify({ documentId: docRef.id }),
            headers: { 'Content-Type': 'application/json' },
            status: 200
        };
    } catch (error) {
        // Handle any errors that occur during processing
        console.error(error);
        return {
            body: JSON.stringify({ error: 'Failed to store unique URL' }),
            headers: { 'Content-Type': 'application/json' },
            status: 500
        };
    }
}

我看到一些人更新了firebase函数来让它工作,但似乎这不适用于我,因为我已经在版本9。
以下是我收到的错误:

[2023-04-13T06:09:01.286Z]  @firebase/firestore: Firestore (9.19.1): GrpcConnection RPC 'Write' stream 0x48b21b62 error. Code: 13 Message: 13 INTERNAL: Received RST_STREAM with code 2 triggered by internal client error: Protocol error
qyyhg6bp

qyyhg6bp1#

答案就在我问的另一个问题里
长话短说:在node.js中运行时,我们必须使用来自"firebase-admin"的库,而不是来自"firebase"的库
另外,仿真器没有初始化,在初始化firebase之前只需要设置一个环境变量。

相关问题