Netlify Function not receiving values

  • we need to know your netlify site name. Example: gifted-antelope-58b104.netlify.app

I’m using 1 Lambda function actively running in production on the site - but when trying to submit a POST request via the endpoint, I am getting a 502 error on the client-side and the following via the Netlify function log -

1:47:35 PM: 2020-12-26T18:47:35.641Z 9f802e38-7d53-49a9-ab66-907fe83315ca ERROR Invoke Error {“errorType”:“TypeError”,“errorMessage”:“Cannot read property ‘split’ of undefined”,“stack”:[“TypeError: Cannot read property ‘split’ of undefined”," at Runtime.exports.handler (/var/task/send-messages.js:3:41)“,” at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}

This seems to be saying that my values are not being passed to the function, but logging the lambda does show them being captured in the body.

Relatively new to using lambda functions via Netlify, so I am probably missing something simple.

Hey @maxspangler,
Would you mind sharing your function code? At first glance, this seems like an error with how split() is being used.

Sure. below is the Send Message function -

exports.handler = function (context, event, callback) {
console.log(context, event)
const phoneNumbers = event.recipients.split(’,’).map((x) => x.trim());
const message = event.message;
const passcode = event.passcode;

if (passcode !== context.PASSCODE) {
const response = new Twilio.Response();
response.setStatusCode(401);
response.setBody(‘Invalid passcode’);
return callback(null, response);
}

const client = context.getTwilioClient();
const allMessageRequests = phoneNumbers.map((to) => {
return client.messages
.create({
body: message,
})
.then((msg) => {
return { success: true, sid: msg.sid };
})
.catch((err) => {
return { success: false, error: err.message };
});
});

Promise.all(allMessageRequests)
.then((result) => {
return callback(null, { result });
})
.catch((err) => {
console.error(err);
return callback(‘Failed to fetch messages’);
});
};

Nice, thanks! I’d log event.recipients to see if you’re getting the value you expect there:

exports.handler = function (context, event, callback) {
console.log(context, event)
console.log(event.recipients)
const phoneNumbers = event.recipients.split(’,’).map((x) => x.trim());
const message = event.message;
const passcode = event.passcode;

Sorry for the late, reply, Jen.

I’m missing something obvious I’m sure - event.recipients is undefined and the values for both the context and event parameters are [object Object].

Is it the way I’m passing data to the function via fetch?

unction sendMessages(form) {

const data = {
passcode: form.passcode.value,
message: form.message.value,
recipients: recipients.join(‘,’),
};
console.log(data)
clearForm(form);

fetch(‘https://www.maxspangler.com/.netlify/functions/send-messages’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify(data),
})
.then((resp) => {
if (resp.ok) {
return resp.json();
} else {
if (resp.status === 401) {
throw new Error(‘Invalid Passcode’);
} else {
throw new Error(
‘Unexpected error. Please check the logs for what went wrong.’
);
}
}
})
.then((body) => {
const successCount = body.result.reduce((currentCount, resultItem) => {
return resultItem.success ? currentCount + 1 : currentCount;
}, 0);

  resultSection.innerText = `Sent ${successCount} of ${body.result.length} messages. Check logs for details`;
})
.catch((err) => {
  resultSection.innerText = err.message;
});

}

I’m on mobile, so please excuse my terseness.

I think it’s the event.body property that holds client-sent data on the server. So it would be event.body.recipients server-side.

1 Like

Hey @maxspangler,
Curious to hear how this one ended up! Did you give @rendall’s suggestion here a try? Instead of logging event.recipients, log event.body.recipients on the Netlify Function side?

1 Like