更新
vehicleDocument、employeeDocument和routeDocument是我的消防仓库中的文档参考。我还根据Renaud更新了我的代码(添加了一个employee_ID查询(,但结果仍然相同。
数据模型为:
---集合:工作分配
------someDocument
--------字符串:employee_ID
----------引用:employae_Document
--------引用:route_Document
--------参考:vehicle_Document
----集合:员工
----someEmployee
---------------字符串: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
这个引用时,它会返回DocumentSnapshot
,您仍然需要再次调用data()
(引用(。我认为有很多可能的纠正方法,但对我来说最简单的是添加.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中工作。