javascript - 云函数返回 NULL
问题描述
我正在构建一个应该从 Firestore 返回文档快照的云功能。在云函数日志中,它控制台记录文档中的数据,但是当我从 React-Native 调用它时,它返回 null。
这是函数本身的代码。
export const getUserProfile = functions.https.onCall((data, context) => {
return new Promise((resolve, reject) => {
const info = admin
.firestore()
.collection("users")
.doc("za5rnpK69TQnrvtNEsGDk7b5GGJ3")
.get()
.then((documentSnapshot) => {
console.log("User exists: ", documentSnapshot.exists);
if (documentSnapshot.exists) {
console.log("User data: ", documentSnapshot.data());
documentSnapshot.data();
}
});
resolve(info);
});
});
还添加来自 React-Native 的代码来调用该函数。
functions()
.httpsCallable("getUserProfile")({})
.then(r => console.log(r));
解决方案
你没有正确处理承诺。没有理由new Promise
在这里拥有。(事实上,它很少需要——只有当你调用的 API 不使用 Promise 而只使用回调函数时。)如果你试图将文档的内容返回给调用者,这就是你所需要的。 :
return admin
.firestore()
.collection("users")
.doc("za5rnpK69TQnrvtNEsGDk7b5GGJ3")
.get()
.then((documentSnapshot) => {
if (documentSnapshot.exists) {
return documentSnapshot.data();
}
else {
return { whatever: 'you want' }
}
});
如果没有文件,您必须决定您希望客户收到什么。
推荐阅读
- python - 商业情感词典
- javascript - 等待承诺而不继续执行
- json - 从 Scala 中的 CSV 列中发现类型
- javascript - 如何使用 javascript(Reactjs) 作为前端和 node.js 作为后端创建依赖下拉列表
- python - 我无法导入 data_utils
- sql - 我如何获得重叠值的计数并按最多重叠的百分比排序
- java - 扩展HashMap的java类
> 覆盖方法不工作 - google-apps-script - 如何将文件从我的 Google Drive 下载到我的本地下载文件
- r - 如何修复在 Linux/Fedora 31 上的 R 中未正确显示的度数符号
- java - 在 Spring RestTemplate 和 ResponseEntity 中设置状态码 301 或 303