简体   繁体   English

Node.js Wit.ai许诺返回函数

[英]Node.js Wit.ai promise return function

My promise return undefined values. 我的诺言返回未定义的值。 I'm not sure how I can perform this function correctly in my node.js page. 我不确定如何在我的node.js页面中正确执行此功能。 Could you please help me with this in order to return all values AFTER geocoding in my context in good way! 能否请我帮忙,以便在上下文中以良好的方式返回所有值! Thank you 谢谢

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);
    });
}

You don't need to say return resolve(... . Instead you just call resolve(... 您不需要说return resolve(...相反,您只需调用resolve(...

For example: 例如:

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!");
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM