作成中の Node 天気アプリについて助けが必要です。Google API を使用して住所の緯度と経度を取得し、これを Dark Sky API にフィードして天気を JSON 形式で返しています。
私の問題は、ほとんどの場合、Google API が経度と緯度を返す前に Weather API が検索を行うことです。Node の非同期性のために、なぜそれを行うのかはわかっています。私の質問は、代わりにこの部分をどのように同期させるのですか?
var express = require('express');
var https = require('https')
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var lon;
var lat;
app.post('/', function(req,res,next){
var search = req.body.search;
function google(){
var api = "API-KEY";
var url = https.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${search}&key=${api}`, function(res3){
var body = "";
var googleResults;
res3.on('data', function(data){
body += data.toString();
})
res3.on('end', function(){
googleResults = JSON.parse(body);
//console.log(body);
lat = googleResults.results[0].geometry.location.lat;
lon = googleResults.results[0].geometry.location.lon;
console.log(search);
})
});
}
google();
//connectc to API URL()
var request = https.get(`https://api.darksky.net/forecast/API-KEY/${lon},${lat}`, function(res2){
console.log(lon + " " + lat)
var body = "";
var weather;
//Read the data
res2.on('data', function(data){
body += data.toString();
})
res2.on('end', function(){
//Parse the data
weather = JSON.parse(body);
//Print data
//console.log("test = " + weather.currently.temperature)
res.render('index', {
temperature: weather.currently.temperature,
humidity: weather.currently.humidity,
wind: weather.currently.windSpeed
});
})
});
});
コードをあまり再構築せずに行う最も簡単な方法は、google()
コールバック関数を引数として取ることです。次に、そのコールバック関数を使用して、暗い空への呼び出しをトリガーできます。
var express = require('express');
var https = require('https')
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/', function(req, res, next) {
var search = req.body.search;
// Have google() take a callback function as an argument and
// call that function with the data when it's done.
function google(cb) {
var api = "API-KEY";
var url = https.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${search}&key=${api}`, function(res3) {
var body = "";
var googleResults;
res3.on('data', function(data) {
body += data.toString();
})
res3.on('end', function() {
googleResults = JSON.parse(body);
//console.log(body);
lat = googleResults.results[0].geometry.location.lat;
lon = googleResults.results[0].geometry.location.lon;
// Call the function here
cb(lat, lon);
})
});
}
// Now use the callback to do your second request to darksky
google(function(lat, lon) {
//connectc to API URL()
var request = https.get(`https://api.darksky.net/forecast/API-KEY/${lon},${lat}`, function(res2) {
console.log(lon + " " + lat)
var body = "";
var weather;
//Read the data
res2.on('data', function(data) {
body += data.toString();
})
res2.on('end', function() {
//Parse the data
weather = JSON.parse(body);
//Print data
//console.log("test = " + weather.currently.temperature)
res.render('index', {
temperature: weather.currently.temperature,
humidity: weather.currently.humidity,
wind: weather.currently.windSpeed
});
})
});
}});
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加