在GCP云函数中使用@google-cloud/firestore连接非默认Firestore数据库
解决@google-cloud/firestore连接非默认Firestore数据库的问题
你当前代码的问题在于参数名错误,在@google-cloud/firestore v6.7.0版本中,指定非默认数据库需要使用databaseId参数,而非你写的database。
修正后的代码示例:
const { Firestore } = require('@google-cloud/firestore'); const firestore = new Firestore({ projectId: "my-gcp-project", databaseId: "my-db" // 使用正确的参数名指定目标数据库 });
额外注意事项:
- 确保Cloud Function使用的服务账号拥有目标非默认数据库的访问权限,需在IAM中为该账号配置对应数据库的Firestore读写角色
- 若在GCP内部环境运行(如Cloud Function),
projectId可省略,SDK会自动从环境变量中获取项目ID
内容的提问来源于stack exchange,提问作者Igorov
相关产品推荐
相关产品推荐

