![](/img/trans.png)
[英]How can i access Context in _app.js file in nextjs in react js
[英]how can I access to something in api to my app.js file?
在这个api中,我想从forecast.js文件访问图标文件到app.js文件。 但我做不到。 因为我想在 html 文件中显示图标
接口响应:
{
(...)
"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 中 res.send 但我无法从那里访问它。 我该怎么做?
两件事情。 一,从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 );
});
不同之处在于我将图标作为第三个参数。
第二,您现在可以在预测调用后在 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,
})
})
我不确定他们的 API 是否有一些具有“正确”方式的文档,但是您始终可以以与它们相同的方式利用图标。 他们的顶级域 (darksky.net) 有一个包含实际文件的images/weather-icons
文件夹。
//darksky.net/images/weather-icons/{icon-name.ext}
您的问题是用 Node 标记的,所以您可能需要下载文件? 或者只是使用上述来源创建一个图像标签。 我不确定你的意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.