Next.js Discord

Discord Forum

Issue with optional chaining in Vercel

Answered
Alι_ Aryαɴ posted this in #help-forum
Open in Discord
I'm facing an issue on Vercel with Node.js. When I access 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:
{
  "version": 2,
  "builds": [
    {
      "src": "./index.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/"
    }
  ]
}

Now it's working fine
View full answer

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:
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 :meow_stare:
what do you see in your vercel logs?
@not-milo.tsx what do you see in your vercel logs?
RequestId: 892bc1a0-2233-4f64-b25f-af2d4a8f2451 Error: Runtime exited with error: exit status 1 Runtime.ExitError
@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 pinner?.full_name Instead of pinner.full_name
@Clown Also where exactly is data coming from?
It's coming from scraping
Hmmm
Now, I changed my code to
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:
{
  "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!