Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase函数未返回正确的数据 更新_Javascript_Firebase_Google Cloud Functions_Return_Function Call - Fatal编程技术网

Javascript Firebase函数未返回正确的数据 更新

Javascript Firebase函数未返回正确的数据 更新,javascript,firebase,google-cloud-functions,return,function-call,Javascript,Firebase,Google Cloud Functions,Return,Function Call,车辆文档、员工文档和常规文档是我的firestore中的文档参考。我还根据Renaud更新了代码(添加了员工ID查询),但结果仍然相同 数据模型为: ---收集:作业 ------someDocument ---------字符串:员工ID ---------参考:员工文档 ---------参考:路线图文档 ---------参考:车辆文档 ---收集:员工 ------一些员工 ---------字符串:employee\u ID 我没有从Firebase函数中获得所需的结果,我总是得到以下

车辆文档、员工文档和常规文档是我的firestore中的文档参考。我还根据Renaud更新了代码(添加了员工ID查询),但结果仍然相同

数据模型为:

---收集:作业
------someDocument
---------字符串:员工ID
---------参考:员工文档
---------参考:路线图文档
---------参考:车辆文档
---收集:员工
------一些员工
---------字符串:employee\u 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语句中获取“inside”,但无论如何它仍然返回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您澄清了您试图用
wait assignmentDocument.data()['employee_Document'].get()做什么
assignmentDocument.data()['employee_Document']
返回字段
employee_Document
的值。它可能是一根绳子。调用异步
get()
方法不会返回任何结果。您是否可以共享您的数据模型以及有关如何使用此云函数的解释。此外,您是否确定应该执行
if(employeeDocument.data()['employee_ID']==uid)
循环测试。在我看来,您可以查询整个集合,然后遍历所有文档以找到具有正确UID的文档。为什么不查询这个唯一的文档?例如,
const snapshot=await admin.firestore().collection(“assignments”).where('employee_ID','=','uid').get()
@RenaudTarnec由于“工作分配”集合中没有员工ID,因此只有对员工文档的引用。因此,为了简单起见,您建议我将员工ID添加到分配文档中?非常感谢,我只是忘记了这一点。这些小错误。。。