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

时间:2017-08-09 16:55:37

标签: javascript async-await

我正在尝试调用ASYNC API,它继续并从远程WebService获取一些数据(我不想使用Fetch / AXIOS库),并且必须从NodeJS路由器方法调用API(我的NodeJS版本是8.1.4),使用Mongoose的一些MongoDB操作存在于代码中(你可以忽略)但每当我调用ASYNC 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...
    ​​===========================================================================================================================

1 个答案:

答案 0 :(得分:4)

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

CC_Trip.find({ trip_id: trip_id, user_id: user_id, vin: lvin }, async function (err, trips) {
// ... etc.
}