Reputation: 2876
I have the following server-side set-up:
router.get(
"/auth/google",
passport.authenticate("google", { scope: ['Profile','https://www.googleapis.com/auth/analytics.readonly'] })
);
router.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
res.redirect("/getData?token=" + token);
}
);
router.get('/getData', function(req, res) {
var token = req.query.token;
request('https://www.googleapis.com/analytics/v3/management/accounts?access_token=' + token,
function (error, response, body) {
let views = []
JSON.parse(body).items.forEach(view => {
views.push({
name: view.webPropertyId + ' - ' + view.name + ' (' + view.websiteUrl + ')'
})
})
res.send(views)
});
})
with the following client-side component:
componentDidMount() {
fetch('http://localhost:5000/getData',
{
method: 'put',
headers: {'Content-Type': 'application/json'}
})
.then(res => {
if (!res.ok) {
throw res;
}
return res.json()
}).then(data => {
this.setState({loading: false, data});
}).catch(err => {
console.error(err);
this.setState({loading: false, error: true});
});
}
how do I'm supposed to configure express so I can fetch my back-end and pass the response from the API request on my front-end?
Right now I'm getting the following error Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Upvotes: 0
Views: 368
Reputation: 307
You need to send the information through JSON. That's res.json(dataObject);
, which will be picked up by the second .then
in the fetch call. You are currently trying to use res.send()
.
Upvotes: 1
Reputation: 11
Maybe try switching the put method to a get in your fetch params - method: 'GET'
Upvotes: 1