2017-01-17 8 views
0

私の約束は未定義の値を返します。 node.jsページでこの機能を正しく実行する方法がわかりません。私のコンテキストでジオコーディングした後、すべての値を適切に返すために、これを手伝ってください。ありがとうございます
Node.js Wit.ai約束返却関数

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

答えて

0

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