You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:19:53