如何解决Firebase 函数没有返回正确的数据
更新
VehicleDocument、employeeDocument 和 routeDocument 是我的 Firestore 中的文档引用。我还根据 Renaud 更新了我的代码(添加了对 employee_ID 的查询),但结果仍然相同。
数据模型是:
---集合:作业
------ 一些文档
--------- 字符串:employee_ID
--------- 参考:employee_Document
--------- 参考:route_Document
--------- 参考: Vehicle_Document
--- 集合:员工
------ 一些员工
--------- 字符串:employee_ID
我的 Firebase 函数没有得到想要的结果,我总是得到这个结果:
{result: tour-found,tourStartTime: null,licensePlate: null,vin: null,waypoints: null,tourcode: null}
来自此代码:
exports.getTourDataFromAssignment = functions.runWith({timeoutSeconds: 15}).region(REGION_ID).https.onCall(async (data,context) => {
//retrieve uid and get assignmentCollection
const uid = data.uid.toString();
var assignmentDocument;
// eslint-disable-next-line promise/always-return
const snapshot = await admin.firestore().collection('assignments').where('employee_ID','==',uid).get();
if(snapshot.docs.length !== 0) {
assignmentDocument = snapshot.docs[0];
}else{
return {
'result': 'no-tour-found',};
}
//loop over assignment,find employeeDocument,get check for identical employeeID
async function getDocumentsFromAssignment() {
const vehicleDocument = await assignmentDocument.data()['vehicle_Document'].get();
const routeDocument = await assignmentDocument.data()['route_Document'].get();
return [routeDocument,vehicleDocument];
}
const result = await getDocumentsFromAssignment();
const routeDocument = result[0];
const vehicleDocument = result[1];
return {
'result': 'tour-found','waypoints': routeDocument["waypoints"],'licensePlate': vehicleDocument["licensePlate"],'vin': vehicleDocument["vin"],'tourcode': routeDocument["tourcode"],'tourStartTime': routeDocument["tour_start_time"],};
我可以确保请求中的 uid 等于我的 Firestore 中的 uid。这意味着我能够进入 if 语句的“内部”,但无论如何它仍然返回 null。
解决方法
这与在 getDocumentsFromAssignment
函数中获取文档有关。当您 get
这个 reference it returns DocumentSnapshot
时,您仍然必须再次调用 data()
(reference)。我认为有很多可能的修正,但对我来说最简单的是添加 .data()
方法作为回报:
async function getDocumentsFromAssignment() {
const vehicleDocument = await assignmentDocument.data()['vehicle_Document'].get();
const routeDocument = await assignmentDocument.data()['route_Document'].get();
return [routeDocument.data(),vehicleDocument.data()];
}
我在 node12 上本地测试了它,所以它应该也可以在 Cloud Function 中工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。