Skip to main content

Hi, I’m having trouble passing an object from React to Express, and then creating an airtable record in Express.


In react, i am sending an http request to Express via:



finalSubmit() {

const airtableObj = {

title: ‘hi’,

}

fetch(‘api/submit’,{

method: ‘POST’,

body: JSON.stringify(airtableObj),

headers: {“Content-Type”: “application/json”}

})

}



In Express, I am doing:



app.post(’/api/submit’, jsonParser, async (req, res) => {

const newStudy = JSON.stringify(req.body);

await console.log(newStudy);

table.create(newStudy, function(err, record) {

if (err) {console.log(err); res.json(err)} else {console.log(record), res.json(‘Success!’)}

});

})



(If I do table.create({“title”:“hi”} instead of table.create(newStudy), everything works fine)


However, I keep getting:



Class {

error: ‘INVALID_REQUEST_UNKNOWN’,

message:

‘Invalid request: parameter validation failed. Check your request data.’,

statusCode: 422 }



Sorry for the newbie question, but is there something wrong with how I am manipulating my data with JSON?

Thanks

This appears to be happening because you’re calling JSON.stringify(req.body), which you don’t need to do.


table.create takes an object, not a string, so you’ll want to do something like this:


const newStudy = req.body;
table.create(newStudy, function(err, record) {
// ...
});

(It looks like you also posted this on StackOverflow, where I gave the same answer.)


Reply