Я пытался сделать свой собственный прогноз погоды.Я взял даркский за данные.Мой сервер - это короткий сервер node.js с геоданными и частью прогноза.
Часть прогноза: ...
const request = require('request');
const forecast = (latitude, longitude, callback) => {
const url =
darksky url
var weatherData = {};
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);
} else {
callback(
undefined,
(weatherData = {
daily: body.daily.data[0].summary,
temp: body.currently.temperature,
regen: body.currently.precipProbability
})
);
}
});
};
module.exports = forecast;
...
сервер:
...
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const geocode = require('./utils/geocode');
const forecast = require('./utils/forecast');
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello World');
});
app.get('/weather', (req, res) => {
console.log(req.query.search);
geocode(req.query.search, (error, { latitude, longitude, location } = {}) => {
if (error) {
return res.send({ error });
}
forecast(latitude, longitude, (error, forecastData) => {
if (error) {
return res.send({ error });
}
const data =({
location,
forecast: forecastData
});
console.log(JSON.stringify(data));
res.send(JSON.stringify(data));
});
});
});
app.listen(port, () => {
console.log("Server is up and running");
});
...
Теперь я пытаюсь получить это в своем приложении 7 angular.
Я сделал услугу:
...
getDataServer(addy: string) {
return this.http.get(this.url + addy).subscribe(responseData => {
console.log(responseData);
});
}
...
Responsedata содержит всю необходимую информацию в этом формате:
{location: Stuttgart, forcast: {daily: 'sun', regen: 0, temp: 17}}
Теперь я попытался получить прогноз в своем компоненте, но это не сработает.Я пытался сделать Observable и подписаться, но ничего не получалось.
Может быть, у вас есть небольшая подсказка,
спасибо,
Том