T
TanStack9mo ago
complex-teal

Issues calling server/ts function in API route

I'm running into this issue in an API route. It seems that I cannot co-locate ts functions in a functions file alongside server functions. This doesn't appear to be an issue when calling server functions not in API routes
Error importing route file: Error: Invariant failed: createServerFn must be called with a function that is marked with the 'use server' pragma. Are you using the @tanstack/start-vite-plugin ?
at invariant (file:///Users/am/Coding/code/node_modules/tiny-invariant/dist/esm/tiny-invariant.js:12:11)
at Object.handler (/Users/am/Coding/code/node_modules/@tanstack/start/dist/esm/client/createServerFn.js:37:40)
at eval (/Users/am/Coding/code/src/web/features/files/functions.ts:63:3)
at async ESModulesEvaluator.runInlinedModule (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1053:5)
at async SSRCompatModuleRunner.directRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1279:61)
at async SSRCompatModuleRunner.directRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:30612:23)
at async SSRCompatModuleRunner.cachedRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1174:76)
at async eval (/Users/am/Coding/code/src/web/routes/api/upload.ts?pick=APIRoute:6:31)
Error importing route file: Error: Invariant failed: createServerFn must be called with a function that is marked with the 'use server' pragma. Are you using the @tanstack/start-vite-plugin ?
at invariant (file:///Users/am/Coding/code/node_modules/tiny-invariant/dist/esm/tiny-invariant.js:12:11)
at Object.handler (/Users/am/Coding/code/node_modules/@tanstack/start/dist/esm/client/createServerFn.js:37:40)
at eval (/Users/am/Coding/code/src/web/features/files/functions.ts:63:3)
at async ESModulesEvaluator.runInlinedModule (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1053:5)
at async SSRCompatModuleRunner.directRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1279:61)
at async SSRCompatModuleRunner.directRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:30612:23)
at async SSRCompatModuleRunner.cachedRequest (file:///Users/am/Coding/code/node_modules/vite/dist/node/module-runner.js:1174:76)
at async eval (/Users/am/Coding/code/src/web/routes/api/upload.ts?pick=APIRoute:6:31)
eval (/Users/am/Coding/cre-tools/src/web/features/files/functions.ts:63:3) points to the function
export async function upsertDealFile(db: DbTx, dealFile: DealFilesInsert) {
const [newDealFile] = await upsertRecord(
db,
dealFiles,
dealFile,
[dealFiles.dealId, dealFiles.fileId],
['dealId', 'fileId', 'id', 'shortId'],
)
return newDealFile
}
export async function upsertDealFile(db: DbTx, dealFile: DealFilesInsert) {
const [newDealFile] = await upsertRecord(
db,
dealFiles,
dealFile,
[dealFiles.dealId, dealFiles.fileId],
['dealId', 'fileId', 'id', 'shortId'],
)
return newDealFile
}
/Users/am/Coding/code/src/web/routes/api/upload.ts?pick=APIRoute:6:31) points to import { uploadFileFn } from '@/web/features/files/functions' which is a server function which uses upsertDealFile.
3 Replies
flat-fuchsia
flat-fuchsia9mo ago
you cannot call server functions in API routes
complex-teal
complex-tealOP9mo ago
it also seems that if you import the ts function which is colocated with server functions, this error will occur. is that expected? I tried it both ways, calling the ts func or server fn and both had the error
flat-fuchsia
flat-fuchsia9mo ago
can you provide a minimal complete example for that please?

Did you find this page helpful?