Next.js Discord

Discord Forum

Server Action is not ran / returns undefined, only on build.

Answered
Spinge Bib Sqorpnts posted this in #help-forum
Open in Discord
Avatar
Hello! In versions newer than 15.0.0-canary.120, not sure in which one specifically this begins to happen, but some server actions simply do not return any value.

for context, I have an async function inside a useEffect that runs on-mount, and calls a function that is in a file with use server at the top. That function calls another server action, which is supposed to return a JSON object, and the first function returns the value of the second function. In that one particular place the second function does log the value it is supposed to return, but the second function doesn't log anything and the one inside the useEffect logs undefined.
Answered by Spinge Bib Sqorpnts
@joulev It seems I wasn't the only one experiencing this:

https://github.com/vercel/next.js/pull/69788
View full answer

45 Replies

Avatar
@joulev it stops working on version 15.0.0-canary.126
.125 is the last version where it works correctly
Avatar
@Spinge Bib Sqorpnts <@484037068239142956> it stops working on version 15.0.0-canary.126
Avatar
yes this is 200% a nextjs bug. please file a bug report.
Avatar
@joulev yes this is 200% a nextjs bug. please file a bug report.
Avatar
See the thing is, opening an issue requires a repro link, and I have no idea why it happens in that specific place and I can't just upload the entire project
Avatar
the flow basically goes like this

// component
...
const [someState, setSomeState] = useState(/* some number */);

useEffect(() => {
  const runFetch = async () => {
    await action1()
  }
}, [someState]);


// action1
'use server';

export async function action1() {
 return await action2();
}


// action2
'use server'

export async function action2() {
  return await fetch(/* */);
}
await action1() returns undefined in my case
also, it reproduces only on builds
it doesn't reproduce dev (turbo or without)
correction, the useEffect does have a dependency
Avatar
Need to go out now, will check this later
Avatar
@Spinge Bib Sqorpnts the flow basically goes like this tsx // component ... const [someState, setSomeState] = useState(/* some number */); useEffect(() => { const runFetch = async () => { await action1() } }, [someState]); tsx // action1 'use server'; export async function action1() { return await action2(); } tsx // action2 'use server' export async function action2() { return await fetch(/* */); }
Avatar
hhmm i can't reproduce though :thinkies:
"use server";

export async function action1() {
  // If you do this, it doesn't work because Response is not a valid
  // server action return value.
  // return fetch("https://generate-secret.vercel.app/32");

  // But you can do this just fine
  const res = await fetch("https://generate-secret.vercel.app/32");
  return res.text();
}

export async function action2() {
  return action1();
}
Image
could you make a minimal reproduction repository?
this is also exactly why all issues on github are required to have reproduction repositories, else they just read your issue and have no clue how that happened
Avatar
yup, take your time
Avatar
@joulev this is also exactly why all issues on github are required to have reproduction repositories, else they just read your issue and have no clue how that happened
Avatar
I'll start investigating now, but about this, while I get the idea, it becomes more difficult to treat edge-case problems in big projects that cannot be entirely reproduced due to it being for example, work property
Avatar
okay I have a lead!
kind of at least
the code used to be

// component
/* ... */
useEffect(() => {
  async function GetData() { /* ... */ }
  GetData();
}, [someState]);


But when I changed it to:
// component

/* ... */
async function GetData() { /* ... */ }
// Technically async callbacks are not allowed in useEffect 🤷‍♂️
// @ts-ignore
useEffect(GetData, [someState]);

It works!
I have no idea why, and I still think it's a bug, unless I broke a react rule by defining and calling a function inside a useEffect
Avatar
wha- how is this even possible
Avatar
I really don't know
Avatar
@Spinge Bib Sqorpnts I'll start investigating now, but about this, while I get the idea, it becomes more difficult to treat edge-case problems in big projects that cannot be entirely reproduced due to it being for example, work property
Avatar
you can use this method to make a minimal example:
* remove unrelated parts
* remove things bit by bit
* there will come a time when the bug goes away – the bit that makes the bug goes away is the critical bit
i find it hard to make a minimal repro from a blank repo sometimes, but with this method though it takes more time and effort, it always works
Avatar
I'll try to make something up
Avatar
yeah certainly that's a huge lead, not enough to know what's wrong but enough to know it has something to do with useEffect
Avatar
@joulev that's funny
Image
Imma try useEffect(() => GetData())
Avatar
Okay doing useEffect(() => { GetData(); }, [someState]); works
so I think it's about the Function Definition
I wonder if it's because some of the tree is dynamically imported?
nope
Avatar
@joulev It seems I wasn't the only one experiencing this:

https://github.com/vercel/next.js/pull/69788
Answer
Avatar
I tested out latest canary and it works now
Avatar
nice to see, wow tricky bug