Hello!
I have a Netlify site at www.icecontractormn.com. I am a Netlify newbie and a relatively new dev, so I suspect I am missing something basic.
I am trying to build a function that will take data from a form I built and then send it to a third party CMS. The POST I am making keeps throwing a 500 error and I can’t for the life of me figure out what the cause is.
Here is my client side code:
axios({
data: JSON.stringify({
"customer": {
"customer_first_name": "John",
"customer_last_name": "Doe",
"customer_email": "johndoe@zuper.com",
"customer_category": "5f1a48d11289d5e82e798167"
}
}),
method: 'POST',
url: '/.netlify/functions/addCustomer',
}).then(response => {
console.log('response.json();', response.json());
response.json();
});
And here is my function:
const axios = require("axios");
exports.handler = async function (event, context) {
// Only allow POST
if (event.httpMethod !== "POST") {
return { statusCode: 405, body: "Method Not Allowed" };
}
console.log('hit my netlify event.body', event.body);
return axios({
method: "POST",
url: "https://staging.zuperpro.com/api/customers",
headers: {
"content-type": "application/json",
"x-api-key": process.env.ZUPERPRO,
},
data: JSON.stringify(event.body),
}).then((response) => {
console.log("success", response);
return {
statusCode: 200,
body: JSON.stringify(response)
}
}).catch((e) => {
console.log('got an error', e.message);
return {
statusCode: 500,
body: JSON.stringify({
error: e.message
})
}
});
};
Finally, here are the errors I am seeing.
From the browser:
And from my terminal:
Oh, and while my site is in production this code is still only local.
Any help is appreciated!
Thank you!