2017-02-10 8 views
0

私はWit AIチュートリアルに従っています。 私は実際の天気APIを呼び出すためにクイックスタート天気チュートリアルを拡張しようとしていますが、私は運がないです。Wit AIと非同期関数

ここに私の変更されたgetForecastメソッドがあります。オリジナルは、ここで見つけることができます:https://wit.ai/docs/quickstart

var weather = require('weather-js'); 
 

 
    const actions = { 
 
    send(request, response) { 
 
    const {sessionId, context, entities} = request; 
 
    const {text, quickreplies} = response; 
 
    console.log('sending...', JSON.stringify(response)); 
 
    }, 
 
    getForecast({context, entities}) { 
 
    var location = firstEntityValue(entities, 'location'); 
 
    weather.find({search: location, degreeType: 'F'}, function(err, result) { 
 
     if(err){ 
 
      context.missingLocation = true; 
 
      delete context.forecast; 
 
     }else{ 
 
      var temperature = result[0].current.temperature; 
 
      context.forecast = temperature+ ' degrees in '+location; // we should call a weather API here 
 
      delete context.missingLocation; 
 
      fs.writeFile("weather.json",JSON.stringify(result)); 
 
     } 
 
     return context; 
 
    }); 
 
    }, 
 
};

答えて

0

だからあなたがAPIを乗り切るために同期呼び出しをしたいけど、ノードがそれを奨励していません。同期呼び出しを取得する方法は、約束を使用することです。

参考 - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

また、以下の気象ボットのウィットの独自の実装へのリンクがあり、あなたが約束が使用されているかを確認することができます

https://github.com/wit-ai/witty-weather/blob/master/src/createEngine.js