0
I made this code based on a React project. Using Axios and React Native, I am trying to return the data from the array, but it is only returning in the log an error 400 in the request of the api.
Here is the code:
main.js
import React, {Component} from 'react';
import Weather from '../services/weather';
import axios from 'axios';
import{ View, Text, TextInput, Button } from 'react-native';
export default class Main extends Component {
static navigationOptions = {
title: "Open Weather"
}
constructor() {
super();
this.state = {
weather: [],
temp: [],
clouds: []
}
}
getWeather = query => {
axios.get(`https://api.openweathermap.org/data/2.5/find?q=${query}&units=metric&appid=6724e5bbc20ecbdb04109535892e5e49`)
.then(response => {
this.setState({
weather: response.data.list[0],
temp: response.data.list[0].main.temp,
clouds: response.data.list[0].weather[0].description
});
})
.catch(error => {
console.log('Error', error);
});
};
queryWeather = (event, cityName) => {
event.preventDefault();
cityName = event.target.value;
this.getWeather(cityName);
}
render(){
return(
<View>
<Text>Cidade: </Text>
<TextInput queryWeather={this.queryWeather}
style={{ height: 40, borderColor: '#dfdfdf', borderWidth: 1 }}
placeholder={'Digite o nome da cidade...'}
/>
<Button title="Buscar" onPress={this.queryWeather}/>
<Weather
city={this.state.weather.name}
temp={this.state.temp}
clouds={this.state.clouds}
/>
</View>
);
}
}
Weather.js
import React from 'react';
import { Text, View } from 'react-native';
const Weather = props =>
<View>
<Text>Cidade: {props.city}</Text>
<Text>Temperatura: {props.temp}</Text>
<Text>Céu: {props.clouds}</Text>
</View>
export default Weather;