Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling server function within another server function inside loader alters parameters unexpectedly #1866

Closed
jadedevin13 opened this issue Jun 30, 2024 · 1 comment · Fixed by #2766
Labels
start Everything about TanStack Start

Comments

@jadedevin13
Copy link
Contributor

jadedevin13 commented Jun 30, 2024

Describe the bug

When calling a server function inside another server function, the parameters get altered unexpectedly, causing the outer function to receive modified parameters. This issue prevents the function from being reusable as intended.

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

  1. Define two server functions using createServerFn:
export const serverFn = createServerFn('POST', async (params, ctx) => {
  console.log(params)
  await server2Fn(params)
  return null
})

export const server2Fn = createServerFn('POST', async (params, ctx) => {
  console.log(params)
  return null
})
  1. Call the serverFn function with the following parameters:

    // inside a loader
    await serverFn({ slug: { eq: 'test' } });
  2. Observe the console logs for params within serverFn:

    Expected output:

    { "slug": { "eq": "test"} }

    Actual output:

    {
      "method": "POST",
      "payload": { "slug": { "eq": "test" } },
      "requestInit": undefined
    }

Expected behavior

The createServerFn wrapper seems to be adding extra data to the parameters, such as method, payload, and requestInit. This behavior interferes with the intended parameter structure, making it difficult to reuse the server function properly.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: "@tanstack/react-router": "^1.43.3", "@tanstack/start": "^1.43.3"

Additional context

No response

@jadedevin13 jadedevin13 changed the title Calling server function within another server function alters parameters unexpectedly Calling server function within another server function inside loader alters parameters unexpectedly Jul 1, 2024
@jadedevin13
Copy link
Contributor Author

jadedevin13 commented Jul 1, 2024

I found that simply replacing the following lines

return Object.assign(
async (payload: TPayload, opts?: FetcherOptions) => {
return compiledFn({
method,
payload: payload || undefined,
requestInit: opts?.requestInit,
})
},

with

  return Object.assign(
    async (payload, opts) => {
      return compiledFn(payload, opts);
    },

Solves my issue and doesn't cause issues with other server functions even when called in the react component. So I'm not sure why it's being called with those extra args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
start Everything about TanStack Start
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants