从Shell在本地对Firestore的调用正在工作,但在部署到Cloud Functions后会给出空错误

时间:2020-09-18 12:22:07

标签: google-cloud-firestore google-cloud-functions google-admin-sdk firebase-cli firebase-tools

我正在从Firebase Functions在Firestore上运行查询。当我从本地shell运行该函数时,一切正常。

但是,在部署代码并调用函数后,Firestore在生产中返回空错误{}。

在这里,Firestore权限不应该成为问题,因为它在本地可用。

服务帐户也不应该成为问题。

没有错误消息或调试信息。我也在服务器控制台上检查了功能日志。

firestore.collection("users").doc(uid).set(userJson, {merge:true})
    .then((done) => {
        console.log('Created new user');
    }).catch(err => {
        console.log('Error creating new user:', JSON.stringify(err));
    });

Package.json

"engines": { "node": "8" }

我希望部署后的行为与外壳中的行为相同。如何从Firebase Functions服务器获得对Firestore工作的调用?

1 个答案:

答案 0 :(得分:1)

  1. 检查 / functions 文件夹中的 package.json 文件(由firebase init创建)。确保您使用的是最新支持的NodeJS版本。从2020年开始,应该是NodeJS 10:

    "engines": {"node": "10"}

  2. 确保您使用的是最新版本的 Firebase CLI 。到2020年应该是8.10.0

    npm install -g firebase-tools

npm list -g --depth=0 
npm uninstall -g firebase-tools
npm install -g firebase-tools
npm list -g --depth=0

如果它不起作用,请先npm cache clean --force清理缓存,然后重试。

  1. 确保您要传递到Firestore查询中的有效载荷没有空白键未定义的值
firestore.collection("users").doc("123").set({
    id: 1234,
    '': 'something', // ERROR
    phone: undefined, // ERROR
    email: 'user@email.com'
})
  1. Firestore调用是异步(它们返回一个Promise)。确保在Firestore调用完成之前没有返回响应response.send()。正确使用async/await.then()

注意:您可能不需要显式配置 Admin SDK服务帐户,因为默认服务帐户已经具有从Cloud Functions访问Firestore的权限。