I'm trying to create a react app that calls an api in an express server and displays the data. There are buttons that a user could press that have a value which would get passed into the api and the proper results would be shown.
My problem is passing the button value from react to express. I have tried req.params.query but it returns undefined. Note that the button values come from a different component.
App.js
class App extends Component {
state = {
data: {},
};
search = async (query) => { <-- query returns button value
fetch('/api')
.then((res) => res.json())
.then((data) => console.log(data)); // console log for now
};
render() {
const {data} = this.state;
return (
<div>
<div className={styles.container}>
<Tables data={data} />
</div>
</div>
);
}
}
server.js
const url = GET_CHANNEL_ID + 'hard coded result' + API_KEY;
app.get('/api', async (req, res) => {
console.log(req.params.query); <-- undefined
request(url, (error, response, body) => { <-- this works fine
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
res.send(info.items[0]);
console.log(info);
}
});
});