Help

Welcome to the Airtable Community! If you're new here, check out our Getting Started area to get the most out of your community experience.

Creating record with an object in React and Express

Topic Labels: API
1551 1
cancel
Showing results for 
Search instead for 
Did you mean: 

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

1 Reply 1

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.)