Answer the question
In order to leave comments, you need to log in
How to access ?
Hello, I'm trying to make an API request to get a photo of the city I specified, and if the request is not successful, then the failurePhotos() dispatcher.
But the problem is that instead of the value I need, response.json() represents itself as an object, where the property I need is indicated by , which cannot be accessed.
Here is the code. Please help.
export function receivePhotos(json, value) {
return {
type: "RECEIVE_PHOTOS",
photos: json,
value: value,
receivedAt: Date.now(),
}
}
export function failurePhotos() {
return {
type: "FAILURE_PHOTOS",
photos: "http://tutinteresno.com/wp-content/uploads/2015/10/nrgruplxira.jpg",
receivedAt: Date.now(),
}
}
export function fetchPhotos(valueLower, value) {
return function(dispatch) {
dispatch(requestPhotos())
var request = new Request
('https://api.teleport.org/api/urban_areas/slug:'+valueLower+'/images/', {
method: 'GET',
mode: 'cors'
});
return(
fetch(request).then(response => {
if (response.ok) {
// console.log("response.json", response.json())
dispatch(receivePhotos(response.json(), value))
} else {
dispatch(failurePhotos())
}
})
)
}
}
Answer the question
In order to leave comments, you need to log in
You are not using the Fetch API
correctly .
Simple usage example:
fetch('https://example.com/api')
.then(response => response.json())
.then(json => dispatch(someAction(json)))
.catch(console.log);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question