Hi everyone,
I’m building a react dictionary app that gets data from the user in a component (Input.js) and sends it to the server.js (app.post). Inside this request I have a nested request to the external API, where it gets the results. I had to put it inside the app.post because of course I don’t have access to the user input I got from the POST request outside of it.
Then, to send the results to the frontend (App.js), I have an app.get request that is called from componentDidMount. The problem is of course that this get request doesn’t have access to the results from the post request, and I cannot nest them or call the get request from inside the then(). localStorage doesn’t work either inside requests.
I had to build a node/express backend to fetch data from the API because I had a bunch of issues with Authentication and CORS and this was the only thing that worked.
Can anyone help me with this? I’m a newbie and I’m getting a headache already. Thanks in advance!
Here’s my server.js code:
const express = require('express');
const app = express();
const request = require('request');
const port = process.env.PORT || 5000;
var rp = require('request-promise');
app.use(express.static('public'));
require('dotenv').config();
// create a POST route
function makeUrl(word_id, baseUrl, source_lang) {
return baseUrl + source_lang + '/' + word_id.toLowerCase();
}
app.post('/express_backend/:word_id', (req, res) => {
//console.log(req.params)
res.send('success')
var word_id = req.params.word_id;
var baseUrl = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/';
var source_lang = 'en';
var url = makeUrl(word_id, baseUrl, source_lang);
url = encodeURI(url);
//console.log('url:', url)
var options = {
url,
headers: {
'app_id': process.env.APP_ID,
'app_key': process.env.APP_KEY,
'Host': process.env.HOST,
'Cache-Control': 'no-cache',
'Postman-Token': process.env.POSTMAN_TOKEN
}
};
//get definitions and pronunciation
var results = [];
rp.get(options, function(error, response, body){
if (error) {
return 'Not Found'
}
}).then(function(body){
var data = JSON.parse(body)
var lexicalEntries = data.results[0].lexicalEntries;
lexicalEntries.forEach(function(entry) {
entry.entries.forEach(function(item){ results.push(item.senses[0].definitions) });
//var audioFile = entry.pronunciations.forEach(function(item){console.log(item.audioFile)});
})
console.log(results)
return results;
}).catch(function(error){
if (error.statusCode == 404){
results = 'No entries found for ' + word_id;
console.log(results);
return results;
}
})
});
app.listen(port, () => console.log(`Listening on port ${port}`));
// create a GET route
app.get('/express_backend', (req, res) => {
res.send({ express: results });
});
And in the App.js, I have this to fetch the data from the backend. It worked perfectly before I made the POST request so I know that my only problem is not being able to pass the data from one request to the other:
componentDidMount() {
// Call our fetch function below once the component mounts
this.callBackendAPI()
.then(res => this.setState({ data: res.express }))
.catch(err => console.log(err));
}
// Fetches our GET route from the Express server. (Note the route we are fetching matches the GET route from server.js
callBackendAPI = async () => {
const response = await fetch('/express_backend');
const body = await response.json();
if (response.status !== 200) {
throw Error(body.message)
}
return body;
};