Async function fails to return despite successfully completing

I am only testing this locally at the moment, so don’t have Netlify platform details to share yet, but I have a question about a function that I can’t seem to get to return.

I’ve never had this issue before - A lambda function which completes its tasks, makes its requests and those requests return properly, but the lambda fails to return back to the calling function. The offending lambda function is as follows:

import axios from 'axios'

import { getAuth0AccessToken } from '../lib/getAuth0AccessToken'

exports.handler = async event => {
  if (!event.body) {
    return {
      statusCode: 400,
      body: JSON.stringify({
        statusCode: 400,
        message: 'Bad request',
      }),
    }
  }
  try {
    const { email, password } = JSON.parse(event.body)
    console.log('Hello from createAccount! payload -->', event.body)

    const response = await getAuth0AccessToken()
    const token = await response.access_token
    // console.log('Access token returned. Proceeding to create user...\nToken -->', token)
    const createUserObj = {
      email: email,
      password: password,
      connection: process.env.AUTH0_DB,
    }
    console.log('Object being sent to create user api --> ', JSON.stringify(createUserObj))

    const userResponse = await axios.post(`${process.env.AUTH0_AUDIENCE}users`, createUserObj, {
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
    })
    const user = userResponse.data
    console.log('User has been returned! -->', user)

    return {
      statusCode: 200,
      body: JSON.stringify(user),
      headers: { 'content-type': 'application/json' },
    }
  } catch (err) {
    let statusCode
    let message
    if (err.response) {
      statusCode = err.response.data.statusCode
      message = err.response.data.message
    } else {
      statusCode = 500
      message = 'Unknown error in Auth0 request'
    }
    console.log(`${statusCode} error from Auth0 --> ${message}`)
    return {
      statusCode,
      body: JSON.stringify({
        statusCode,
        message,
      }),
      headers: { 'content-type': 'application/json' },
    }
  }
}

The last console.log() logs out fine, with the user object that comes back from Auth0, totally as expected. The return statement, however, doesn’t seem to fire - The calling function client-side is as follows:

import axios from 'axios'

export const createAccount = async payload => {
  try {
    const response = await axios.post('/.netlify/functions/createAccount', JSON.stringify(payload))
    console.log('Netlify function has returned! Data -->', response.data)
    return response.data
  } catch (err) {
    console.log('Error has been returned to API!', err)
    return new Error(err)
  }
}

The log here after the axios.post() to my lambda function never fires. Instead the XHR request just sits as a pending promise and then fails out when it reaches the timeout. Its the same if the request to Auth0 returns an error – The error is thrown and caught properly, and the log in the lambda function fires, but the request to the lambda function just sits open, rather than being returned and resolved with the error.

Admittedly, I’m not an expert in lambda functions, and I’ve only recently started using them in async mode, but from a pure JS mode, this doesn’t seem to make any sense to me. All the awaited items have completed, the previous statement has completed properly, so the return statement should fire.

I assume I must be missing something, and it’s probably something obvious. I don’t have anyone to bounce these things off nowadays, though… So does anyone have any ideas?

Cheers,

David

1 Like

I can’t really troubleshoot customer code but one suggestion I can make is to simplify the function and add parts back bit by bit until it stops working. This should help you narrow down why your function’s promise isn’t resolving. Hope that helps.

Interestingly, if I use the callback pattern, even keeping the rest of it the same (still using async/await, but using a callback rather than returning an object at the end), it works fine. So that’s what I’ve done for now. I would be interested to figure out why the other way isn’t working though.

Thanks for the input.