问题描述
我正在从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工作的调用?
解决方法
-
检查 / functions 文件夹中的 package.json 文件(由firebase init创建)。确保您使用的是最新支持的NodeJS版本。从2020年开始,应该是NodeJS 10:
"engines": {"node": "10"}
-
确保您使用的是最新版本的 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
清理缓存,然后重试。
- 确保您要传递到Firestore查询中的有效载荷没有空白键或未定义的值。
firestore.collection("users").doc("123").set({
id: 1234,'': 'something',// ERROR
phone: undefined,// ERROR
email: '[email protected]'
})
- Firestore调用是异步(它们返回一个Promise)。确保在Firestore调用完成之前没有返回响应
response.send()
。正确使用async/await
或.then()
。
注意:您可能不需要显式配置 Admin SDK服务帐户,因为默认服务帐户已经具有从Cloud Functions访问Firestore的权限。