Undefined lambaResponse

Hi,

I am using netlify dev in order to test a lamba serverless function from netlify identity admin methods. In particular, I am trying to get the list of users that signed up on my website. (from here: https://github.com/netlify/gotrue-js#get-a-list-of-users)

                exports.handler = async (event, context) => {
                   const {identity, user} = context.clientContext;
                   const usersUrl = `${identity.url}/admin/users`;
                   const adminAuthHeader = 'Bearer ' + identity.token;
                   console.log('event', JSON.stringify(event));
                   console.log('context', JSON.stringify(context));

                  try {
                      const resp = await fetch(usersUrl, {
                      method: 'GET',
                      headers: {Authorization: adminAuthHeader},
                 })
                 const data = resp.json()
                 return data
                } catch (e) {
                     return e;
               }
             };

So I defined my function file called users.js inside my functions directory where I put the above piece of code. I tried both the async/await approach and callback approach. I tried to use both fetch and axios to make the request to the netlify identity url.

And I get the following error:

Your function response must have a numerical statusCode. You gave: $ undefined


This means that for some reason the lambdaResponse is undefined, as I saw how the error is handled in this commit: ’
https://github.com/netlify/netlify-dev-plugin/pull/124/files

Could you please help me with some answers?
Thanks a lot!

Hey there. You want to make sure your function is returning the statusCode in the return

The following code should work:

exports.handler = async (event, context) => {
  const { identity, user } = context.clientContext;
  const usersUrl = `${identity.url}/admin/users`;
  const adminAuthHeader = 'Bearer ' + identity.token;
  console.log('event', JSON.stringify(event));
  console.log('context', JSON.stringify(context));
  let data
  try {
    data = await fetch(usersUrl, {
      method: 'GET',
      headers: {
        Authorization: adminAuthHeader
      },
    }).then((res) => res.json())
  } catch (e) {
    return {
      statusCode: 500,
      body: JSON.stringify({
        error: e.message
      })
    }
  }
  return {
    statusCode: 200,
    body: JSON.stringify(data)
  }
};
1 Like

Thanks @DavidWells, the code is working now but I’ve got another error:
{“code”:401,“msg”:“Invalid token: signature is invalid”}

I have been testing the function locally using netlify dev:

Do you have any idea why? Can you help me with this?

I’m not familiar with the local invocation of these functions are emulated.

I’d suggest deploying this to Netlify and triggering the function after logging into to Netlify identity.

If it works on the live Netlify site, it presumably will work on local env.

Protip: use netlify deploy -p to quickly deploy changes to your app/functions to live Netlify site instead of waiting for builds to complete.

1 Like

It worked after I deployed it to Netlify and tested on browser.
Thanks @DavidWells

1 Like