I'm running into some strange behavior on Lambda running Node 14.x. I have a helper function which the main handler function calls, and it contains an user object defined outside of a try catch block. When I log it, it gives me back an object, but then immediately inside the try catch block, user is undefined. Namely I get this error:
ReferenceError: Cannot access 'user' before initialization
This is not the case for strings and numbers, as demonstrated by id. However, client is also an object, but it's accessible inside the try catch, so I'm confused by this behavior. Would appreciate if someone could provide some insight.
const helperFunction = async (payload, context, pool) => {
let { user } = payload;
let { id } = payload.user;
console.log(user); // <-- this is is defined
let client = await pool.connect();
try {
console.log(client); // <-- this is defined
console.log(id); // <-- this is defined
console.log(user); // <-- this is throws reference error, cannot access before initialization
...
} catch (e) {
console.log(e);
}
My user object looks like this:
{
id: '16',
email: 'example@gmail.com',
subscriptions: [ '', 'a', 'b' ],
iat: 1633376553
}