如何从一个请求url模块函数中获取对象到另一个函数



所以,我在一个多API项目中工作,其中有一部分必须输入地址,因为你想要获得天气输出,而我被困在一个可以获取长时间的API值的部分。和纬度。转换为地址字符串,并将其传递给用于天气输出的API。因为我不能访问一个到另一个的API输出。请求url如下:

request({url:url,json:true}, (error,response) => { 
console.log(response.body.current.weather_descriptions[0] +' The temperature is '+chalk.inverse(response.body.current.temperature)+' but it feels like '+ chalk.inverse(response.body.current.feelslike))
})

ok,我找到了一个使用回调函数的答案,我已经在下面的代码中回答了。

const geoCode = (geoUrl, callback) => {
request({url:geoUrl,json:true}, (error,response) => { 
if(error)
{
console.log('Unable to connect to the server.....')
}
else{
callback(undefined,{
latitude:response.body.data[0].latitude,
longitude:response.body.data[0].longitude
})
}


})
}
geoCode(geoUrl,(error,data) =>
{
var url = 'youur api'
url = url+data.latitude+","+data.longitude
// console.log(url)
request({url:url,json:true}, (error,response) => { 

if(error)
{
console.log('Unable to connect to the server.....')
}
else{
console.log(response.body.current.weather_descriptions[0] +' The temperature is '+chalk.inverse(response.body.current.temperature)+' but it feels like '+ chalk.inverse(response.body.current.feelslike))
}


})
})

最新更新