So I want to pass an integer argument to a function in my backend and have it return the data. I've been looking through the documentation and it seems like there might not be a way for me to pass an argument via the code I currently have. I just want second opinions before I consider using a different approach.
Frontend:
//is there a way for me to pass num to req in the backend?
newMarker = (num) => {
fetch('/api/getMarkers')
.then(res => res.json())
.then(mark => this.setState({ markers: mark }))
}
Backend:
//function where argument needs to be passed to, used in the request below
const fillNewMarker = async function fillNewMarker(num){
let temp;
let data;
await populartimes(markers[num].placeID)
.then(out => {data = out; temp = 'Currently ' + data.now.currently + ' full.'})
.catch(() => {temp = 'There is currently no data available.'});
markers[num].busy = temp;
}
//request
//i need num to be passed to req here
app.get('/api/newMarker', async (req,res) => {
await fillNewMarker(req);
console.log('Retrieve Data For New Marker Complete')
var mark = markers;
res.json(mark);
console.log('Sent Markers');
console.log(markers);
})
I've been working for quite a while so my brain is a little bit fried, there might be a really obvious solution that I have missed - if so, I apologize for my ignorance. Help is appreciated! TIA :)
Fix Attemp #1:
//Front end
newMarker = (num) => {
fetch('/api/newMarker', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(num) // body data type must match "Content-Type"
header
})
.then(res => res.json())
.then(mark => this.setState({ markers: mark }))
}
//method call via button
onButtonClick(){
this.newMarker(6)
//6 for testing
}
//backend
app.get('/api/newMarker', async (req,res) => {
console.log('Request received')
await fillNewMarker(req.body.num);
console.log('Retrieve Data For New Marker Complete')
var mark = markers;
res.json(mark);
console.log('Sent Markers');
console.log(markers);
})