No output from lambda function?

I’m getting some strange behaviour from this function: remembermore-dashboard.netlify.app/.netlify/functions/submit

(add https:// to the front of that to see what I mean. I can’t make this post with that link in it)

The code is simply:

exports.handler = function(event) {
    console.log(event);

    return {
        statusCode: 200,
        body: 'Hello world'
    };
};

When I run this locally using netlify dev, I see the “Hello world” output.

On the live site though, I get an HTTP 200 but an empty body.

I can see from the Function log that the code is indeed executing, but something appears to be truncating the body.

(Side note, when I initially tried to post this, my post failed to appear - probably some spam filter? The topic disappeared and I saw the errors below in the browser console. I could only get this post to appear once I removed the URL)

Try stringifying your output :slight_smile:

body: JSON.stringify('hello world')

I’m not sure why stringifying the body content would make a difference? The Netlify documentation for functions has them returning ordinary strings as output for the body, and I have that working fine in other repos.

Anyway, I gave it a go, and I’m still seeing the same thing. Blank output when served from https://remembermore-dashboard.netlify.app/.netlify/functions/submit, but it works fine when I serve it locally using netlify dev.

IIRC and checking the repo, you’ll also need

exports.handler = function(event, context, callback)

That will have no effect if the function doesn’t use those parameters.

Again, to clarify:

  • I know the function is being called on Netlify, because I can see the invocations in the function log - so the console.log bit works
  • This works as expected locally, I can see the output in both Chrome and curl (whether or not it is output as a JSON string)
  • On the Netlify production deployment, I get the HTTP 200 success code, but the body of the response is empty.

I’ve fixed this now. It was because I was missing the async keyword on the function definition.

This works:

exports.handler = async function(event) {
    console.log(event);

    return {
        statusCode: 200,
        body: 'Hello world'
    };
};

Weird that it worked in netlify dev, maybe that’s a bug.

1 Like

hey alex, if you feel like it’s warranted, an issue on this repo would be great:

Thanks, I’ve logged https://github.com/netlify/cli/issues/1047