Issue with optional chaining in Vercel
Answered
Alι_ Aryαɴ posted this in #help-forum
I'm facing an issue on Vercel with Node.js. When I access
Any insights or solutions would be appreciated. Thanks!
user.name in my code, it sporadically throws errors. However, when I use user?.name, it crashes my app.Any insights or solutions would be appreciated. Thanks!
Answered by Alι_ Aryαɴ
I changed my vercel.json:
Now it's working fine
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}Now it's working fine
30 Replies
can't anyone help with it?
show us some of the relevant code and logs
@not-milo.tsx show us some of the relevant code and logs
Well, it's about the Vercel bug:
Code:
I got error on one request like can't read the property of undefined reading full_name
So, I changed the code like pinner?.full_name
Like this:
But my app stop working all of a sudden
Code:
router.get('/pinterest', async (req, res) => {
const pinterest = data.map(({ pinner }) => ({
pinner: {
name: pinner.full_name
}
})) res.status(200).json(pinterest);
});I got error on one request like can't read the property of undefined reading full_name
So, I changed the code like pinner?.full_name
Like this:
router.get('/pinterest', async (req, res) => {
const pinterest = data.map(({ pinner }) => ({
pinner: {
name: pinner?.full_name
}
})) res.status(200).json(pinterest);
});But my app stop working all of a sudden

what do you see in your vercel logs?
RequestId: 892bc1a0-2233-4f64-b25f-af2d4a8f2451 Error: Runtime exited with error: exit status 1
Runtime.ExitError@Alι_ Aryαɴ Well, it's about the Vercel bug:
**Code:**
js
router.get('/pinterest', async (req, res) => {
const pinterest = data.map(({ pinner }) => ({
pinner: {
name: pinner.full_name
}
})) res.status(200).json(pinterest);
});
I got error on one request like can't read the property of undefined reading full_name
So, I changed the code like pinner?.full_name
**Like this:**
js
router.get('/pinterest', async (req, res) => {
const pinterest = data.map(({ pinner }) => ({
pinner: {
name: pinner?.full_name
}
})) res.status(200).json(pinterest);
});
But my app stop working all of a sudden <:meow_stare:763826437341839422>
Are you doing checks to see if the correct result is being returned in your api?
Also where exactly is data coming from?
If you are using an external api make sure to check you're not getting rate limited or something
@Clown Are you doing checks to see if the correct result is being returned in your api?
I run the same code on heroku, replit, render it works fine
But not for Vercel
Like why coz I just use
But not for Vercel
Like why coz I just use
pinner?.full_name Instead of pinner.full_name@Clown Also where exactly is data coming from?
It's coming from scraping
@Clown If you are using an external api make sure to check you're not getting rate limited or something
I checked everything it's just Vercel bug
Hmmm
Now, I changed my code to
It works without error
pinner ? pinner.full_name : ''It works without error
We can't use optional chaining (?.) In Vercel
I'm even using node 18x
admins should do something about this bug
mods required
European sprat
Asian black bear
Are you even using next.js or is this a home-built function on Vercel?
@Asian black bear Are you even using next.js or is this a home-built function on Vercel?
vercel.json:
{
"version": 2,
"builds": [
{
"src": "index.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "index.js"
}
]
}@European sprat https://vercel.com/help#issues
Can't find support email
Asian black bear
click "Create a case" button, fill out the form
also, since you are using the legacy
builds instead of functions it is probably defaulting to an ancient version of Node.js (certainly without optional chaining)I changed my vercel.json:
Now it's working fine
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}Now it's working fine
Answer
Asian black bear
Glad you got it!