Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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 执行异步/等待时出现意外标识符(节点:8.1.4)_Javascript_Async Await - Fatal编程技术网

Javascript 执行异步/等待时出现意外标识符(节点:8.1.4)

Javascript 执行异步/等待时出现意外标识符(节点:8.1.4),javascript,async-await,Javascript,Async Await,我正在尝试调用一个异步API,该API继续运行并从远程Web服务获取一些数据我不想使用Fetch/AXIOS库,该API必须从NodeJS路由器方法调用我的NodeJS版本为8.1.4,代码中有一些使用Mongoose的MongoDB操作,您可以忽略它,但每当我调用异步API时,我都使用了所有正确的关键字,如ASYNC/AWAIT​我反复得到SyntaxError:意外的标识符,我无法找出什么是错误的 ​----------------------------------------------

我正在尝试调用一个异步API,该API继续运行并从远程Web服务获取一些数据我不想使用Fetch/AXIOS库,该API必须从NodeJS路由器方法调用我的NodeJS版本为8.1.4,代码中有一些使用Mongoose的MongoDB操作,您可以忽略它,但每当我调用异步API时,我都使用了所有正确的关键字,如ASYNC/AWAIT​我反复得到SyntaxError:意外的标识符,我无法找出什么是错误的

​----------------------------------------------------------------------------------------------------------------
​async function GetTripSummary(trip_id, vin, job_id)
{
    return new Promise((resolve, reject) => {
        var tid = "Some-ID";
        var options = {
            "method": "GET",
            "hostname": "automotive.internetofthings.ibmcloud.com",
            "port": 443,
            "path": "/driverinsights/drbresult/tripSummaryList?trip_id=" + trip_id + "&tenant_id=" + tid + "&mo_id=" + vin + "&job_id=" + job_id,
            "headers": {
                "accept": "application/json",
                "content-type": "application/json",
                'Authorization': 'Basic ' + new Buffer("Something" + ':' + "Something").toString('base64')
            }
        };
        var req = https.request(options, function (res) {
            var chunks = [];
            res.on("data", function (chunk) {
                chunks.push(chunk);
            });
            res.on("end", function () {
                var body = Buffer.concat(chunks);
                console.log("[1]Map Matched Data Received From ContextMapping: \n\t")
                console.log(body.toString());
                var data = JSON.parse(body);
                resolve(data);
            });
            res.on('error', function (e) {
                reject('problem with request: ' + e.message);
            });
        });
        req.end();
    });
}
​
​----------------------------------------------------------------------------------------------------------------
​
​

    router.get('/cgcc/trip/start/:vin/:uid/:tripid', async function(req, res)
    {   
        try {
            var lvin = req.params.vin;
            var user_id = req.params.uid;
            var trip_id = req.params.tripid;

            CC_Trip.find({ trip_id: trip_id, user_id: user_id, vin: lvin }, function (err, trips) {
                //! A given user with same phone number exists*/
                if ((trips == undefined) || (trips.length <= 0) || (err != null)) {
                    //! End Processing
                    res.send({
                        "user":
                        {
                            "responseCode": "409",
                            "userId": trip_id,
                            "messasge": "Trip does not exist"
                        }
                    });
                }
                else  //! User Exists
                {
                    if (trips[0].moma_job_status == "SUCCEEDED") {
                        const response = await GetTripSummary(trips[0].trip_id, trips[0].vin, trips[0].moma_job_id);
                        return response;
                    }
                    else {
                        res.send({
                            "user":
                            {
                                "responseCode": "301",
                                "userId": trip_id,
                                "messasge": "Background Analysis still going on"
                            }
                        });
                    }
                }
            });
        }
        catch (e)
        {
            console.log(body.toString());
        }
    });
    ​

​

===========================================================================================================================
    ​I keep on getting this error while compiling
    ​
    ​const response = await GetTripSummary(trips[0].trip_id, trips[0].vin, trips[0].moma_job_id);
                                               ^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier
        at createScript (vm.js:74:10)
        at Object.runInThisContext (vm.js:116:10)
        at Module._compile (module.js:533:28)
        at Object.Module._extensions..js (module.js:580:10)
        at Module.load (module.js:503:32)
        at tryModuleLoad (module.js:466:12)
        at Function.Module._load (module.js:458:3)
        at Module.require (module.js:513:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (D:\PES_CC_POC\app.js:18:13)
        at Module._compile (module.js:569:30)
        at Object.Module._extensions..js (module.js:580:10)
        at Module.load (module.js:503:32)
        at tryModuleLoad (module.js:466:12)
        at Function.Module._load (module.js:458:3)
        at Module.require (module.js:513:17)
    Waiting for the debugger to disconnect...
    ​​===========================================================================================================================

您需要将回调函数声明为async,因为您正在使用await:


您需要将回调函数声明为async,因为您正在使用await:


你是一个救生圈标记,我没有意识到它是从回调上下文调用的,现在我很清楚我是nodejsy的新手你是一个救生圈标记,我没有意识到它是从回调上下文调用的,现在我很清楚我是nodejsp的新手可能重复的
CC_Trip.find({ trip_id: trip_id, user_id: user_id, vin: lvin }, async function (err, trips) {
// ... etc.
}