firebase 9 permission denide from localhost



我正在使用Firebase version 9 (MODULAR)作为nodejs v18中web应用程序的数据库。

当我尝试从localhost访问数据时,我得到下面的错误,但从heroku部署工作就像一个魅力。

[2022-06-26T07:32:22.447Z]  @firebase/firestore: Firestore (9.8.4): Connection GRPC stream error. Code: 7 Message: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
[2022-06-26T07:32:22.447Z]  @firebase/firestore: Firestore (9.8.4): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=permission-denied]: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
[2022-06-26T07:32:22.466Z]  @firebase/firestore: Firestore (9.8.4): Connection GRPC stream error. Code: 7 Message: 7 PERMISSION_DENIED: Permission denied on resource project "my_project",.
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
[FirebaseError: Failed to get document because the client is offline.] {
code: 'unavailable',
customData: undefined,
toString: [Function (anonymous)]
}
Node.js v18.0.0

的背景:web应用程序是一个内部工具,所以我做我自己的身份验证服务器端,然后应用程序对firestore数据库上的一些数据进行一些检查。所以基本上是谁进行查询是应用,而不是用户。这让我觉得我应该使用firestore-admin api的服务帐户。问题是,从文档中提供的代码片段,不适合我..

var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});

我已经将访问规则更改为非常不安全的访问规则:

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
你知道我该怎么做吗?或者为什么它对当地的发展不起作用?由于文档是一个不完整的迷宫的东西,我可能错过了什么…

感谢任何可以帮助的人!(至少要明白为什么)

正如GitHub链接所建议的那样,这个问题可能是由于在新的模块化构建(v9)中默认启用了useFetchStreams。

因此,请相应地修改代码:

import {initializeFirestore} from 'firebase/firestore'
const db = initializeFirestore(firebaseApp, {useFetchStreams: false})

同样,这个错误可能不是因为你正在调试的android设备或模拟器脱机,而是因为"databaseURL"在定义Firebase配置对象的部分中缺少设置。. 我认为如果设置不正确,可能会出现同样的错误。

另外,我建议你看一下这个Stackoverflow案例,它详细讨论了"客户端离线"问题背后的所有可能原因。

但是,如果上面的解决方案没有,建议您通过调用setLogLevel来启用调试日志。

firebase.firestore.setLogLevel('debug');

最新更新