React + Flux, ES6, Babel ActionCreate using json server and super agent, data not responding
Hello, I am trying to use json-server to create a React Flux ES6 api application that I am building. But when I use the super agent node module to make a request from the action creator, the data in the callback is undefined
Here's my code
import Dispatcher from '../Dispatcher';
import Constants from '../Constants';
import request from 'superagent';
export default {
setQuestions(guides) {
Dispatcher.handleViewAction({
type: Constants.ActionTypes.SET_QUESTIONS,
data: guides
});
},
getQuestionsFromServer() {
let self = this;
let destination = 'http://localhost:3000/questionnaires';
// request from json service.
request
.get(destination)
.set({
'X-Requested-With': 'XMLHttpRequest'
})
.end(function(response) {
// response is empty. why???
if (response.ok) {
let guideData;
guideData = response.body;
self.setQuestions(guideData);
}
});
}
};
My network tab says the request is in progress but I cannot access the response in the callback.
+3
source to share
1 answer
I figured out how to make this xhr request without the super-agent node module using fetch es2015. Take a look here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
getQuestionsFromServer() {
let self = this;
let destination = 'http://localhost:3000/questionnaires';
// request from json service.response.json()
fetch(destination)
.then(response => response.json())
.then(data => {
this.setQuestions(data[0].questions);
})
.catch(e => console.log("Error", e));
}
Thank!
0
source to share