Lambda function not returning to calling function despite successful execution
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 <http://axios.post|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' },
}
}
}import axios from 'axios'
export const createAccount = async payload => {
try {
const response = await <http://axios.post|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)
}
}<http://axios.post|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
👍
exports.handler = async function (event, context, callback) {
callback(null, {
statusCode: 200,
headers,
body: ''
})
}callbackpattern, I think you need to
resolvethe async function in order for it to return something
exports.handler = async event => {
return new Promise(async function(resolve, reject) {
resolve({
statusCode: 200,
headers,
body: 'something'
})
}
}Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.