在此api中,我要访问从Forecast.js文件到app.js文件的图标文件。但是我做不到。因为我想在html文件中显示图标
Api响应:
{
(...)
"daily":{
"summary":"No precipitation throughout the week, with high temperatures falling to 64°F on Sunday.",
"icon":"clear-day",
"data":[
{
"time":1565766000,
"summary":"Partly cloudy throughout the day.",
"icon":"clear-day",
(...)
"precipProbability":0.02,
(...)
"temperatureMin":62.97,
"temperatureMinTime":1565794800,
"temperatureMax":77.39,
"temperatureMaxTime":1565823600,
(...)
},
(...)
]
},
(...)
}
来自Forecast.js文件代码
const request = require('request');
const forecast = (latitude, longitude, callback) => {
const url = 'https://api.darksky.net/forecast/a6e1a451373ac75d8c4ef09f5b06892e/' + latitude + ',' + longitude +'?units=si';
request({ url, json: true }, (error, {body, }) => {
if (error) {
callback('Unable to connect to weather service!', undefined)
} else if (body.error) {
callback('Unable to find location', undefined)
}
callback(undefined, body.daily.data[0].summary + ' It is currently ' + body.daily.data[0].temperatureMax + ' Max degress & ' +body.daily.data[0].temperatureMin +' Min degress. There is a ' + body.daily.data[0].precipProbability
+ '% chance of rain. icon: ' + body.daily.icon );
})
};
module.exports = forecast;
来自app.js文件
app.get('/weather', (req,res)=>{
if (!req.query.address){
return res.send({
error:'You must provide an address'
})
}
geocode(req.query.address,(error,{latitude,longitude,location } ={})=>{
if (error){
return res.send({error})
}
forecast(latitude,longitude,(error,forcastData,iconsky)=>{
if (error){
return res.send({error})
}
res.send({
forcast:forcastData,
location,
address: req.query.address,
})
})
})
});
这是geocode.js文件
const request = require('request');
const geocode = (address, callback) => {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + address +
'.json?access_token=pk.eyJ1Ijoic29sb3JhbmdlciIsImEiOiJjanoxb2RwdGowbmF3M2JrbnA0OGI1Nzd2In0.ZLeKIz5F7OlG5GxVaKZj0g&limit=1';
request({ url, json: true }, (error, {body}) => {
if (error) {
callback('Unable to connect to location services!', undefined)
} else if (body.features.length === 0) {
callback('Unable to find location. Try another search.', undefined)
} else {
callback(undefined, {
latitude: body.features[0].center[0],
longitude: body.features[0].center[1],
location: body.features[0].place_name
})
}
})
};
module.exports = geocode;
我想访问api中的图标以在app.js中重新发送,但无法从那里访问它。我该怎么办?
答案 0 :(得分:1)
两件事。一,调整来自Forecast.js的回调,而不是
callback(undefined, body.daily.data[0].summary + ' It is currently ' + body.daily.data[0].temperatureMax + ' Max degress & ' +body.daily.data[0].temperatureMin +' Min degress. There is a ' + body.daily.data[0].precipProbability
+ '% chance of rain. icon: ' + body.daily.icon );
})
像这样回叫:
callback(undefined, body.daily.data[0].summary + ' It is currently ' + body.daily.data[0].temperatureMax + ' Max degress & ' +body.daily.data[0].temperatureMin +' Min degress. There is a ' + body.daily.data[0].precipProbability
+ '% chance of rain.', body.daily.icon );
});
区别在于我将icon作为第三个参数。
第二,您现在可以在预测调用后在app.js中访问它:
forecast(latitude,longitude,(error,forcastData,iconsky)=>{
if (error){
return res.send({error})
}
res.send({
forcast:forcastData,
location,
address: req.query.address,
icon: iconsky,
})
})
答案 1 :(得分:0)
我不确定他们的API是否有一些文档具有“正确”的方式,但是您始终可以以相同的方式利用图标。他们的顶点域(darksky.net)有一个包含实际文件的images/weather-icons
文件夹。
//darksky.net/images/weather-icons/{icon-name.ext}
您的问题已用Node标记,因此您可能需要下载文件?或仅使用上述来源创建图片标签。我不确定你的意图。