【问题标题】:Node.js Wit.ai promise return functionNode.js Wit.ai 承诺返回函数
【发布时间】:2017-01-17 08:17:01
【问题描述】:

我的承诺返回未定义的值。我不确定如何在我的 node.js 页面中正确执行此功能。您能否帮我解决这个问题,以便在我的上下文中以良好的方式返回地理编码后的所有值!谢谢

merge_location({
    entities,
    context,
    message,
    sessionId
}) {
    return new Promise(function(resolve, reject) {
        var location = firstEntityValue(entities, 'location');
        if (location) {
            geocoder.geocode(location).then(function(res) {
                console.log(res);
                context.location = location;
                context.lat = res[0].latitude;
                context.lng = res[0].longitude;
                delete context.MissingLocation;
            }).catch(function(err) {
                context.MissingLocation = true;
                delete context.location;
                delete context.lat;
                delete context.lng;
                console.log("Il n'y a pas de ville ");
            });
        } else {
            context.MissingLocation = true;
            delete context.location;
            delete context.lat;
            delete context.lng;
            console.log("Il n'y a pas de ville ");
        }
        console.log("I want to return this" + context.location + ' ' + context.lat + ' ' + context.lng);
        return resolve(context);
    });
}

【问题讨论】:

    标签: javascript node.js wit.ai wit-ai


    【解决方案1】:

    你不需要说return resolve(...。相反,您只需致电resolve(...

    例如:

    function myPromiseFunction(x) {
        return new Promise(function (resolve, reject) {
            // Do your lengthy processing with x here
    
            if (everyThingLooksGood) {
                resolve(thingYouWantToReturn);
            } else {
                reject("ERROR: Things didn't go according to plan!");
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2017-05-15
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2020-05-23
      • 2022-01-26
      • 2019-05-25
      相关资源
      最近更新 更多