我需要有关我正在制作的 Node 天气应用程序的帮助。我使用 Google API 获取地址的经度和纬度,并将其输入到 Dark Sky API 中,以 JSON 格式返回天气。
我的问题是,大多数时候天气 API 都会在 google API 返回经度和纬度之前进行搜索。我知道为什么这样做是因为 Node.js 的异步特性。我的问题是如何使这部分同步?
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
});
})
});
}});
});
关于javascript - 具有 Node.js 异步的 API 数据并检索数据使其同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44076683/