I am trying to use AirTable as part of a node.js app to lookup users. Here is how it should work:
- The table has multiple columns, including one for ID.
- The API looks up the user ID by listing all and filtering by ID.
- If there is no record found for the user ID, it will create a new record for it.
I tried to attempt this using the code below, and it works fine when a user is found, but when it doesn’t, it outputs a 422 error with the status text “Unprocessable Entity”. I would really appreciate if someone could show me how to do this, or show me what I did wrong in my code. (The code is in Node.js and I used request and axios because I was copying existing code that was in Node.js request and I only know how to use Axios)
request({
headers: {'Authorization': 'Bearer <TOKEN>'},
url: `https://api.airtable.com/v0/<APPID>/Users?maxRecords=1&filterByFormula=${encodeURI(`{ID}=${<ID OF USER>}`)}`,
json: true
},function (error, response, body) {
if(!body.records[0]) {
console.log("NOT FOUND")
axios({
method: 'post',
url: `https://api.airtable.com/v0/<APPID>/Users`,
headers: {'Authorization': 'Bearer <TOKEN>'},
data: {
"fields": {
"<field name redacted>": <number>,
"<field name redacted>": <text>,
"<field name redacted>": <number>,
"<field name redacted>": <number>,
"<field name redacted>": <number>,
"<field name redacted>": <number>,
"<field name redacted>": <number>
}
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
} else {
console.log("FOUND")
}
})