#error: a "use server" file can only export async functions, found object.

1 messages · Page 1 of 1 (latest)

pallid rose
#

I am using next 14 app router and currently have the error pointed towards one of my route files.

"use server";

import { NextResponse } from "next/server";

import { AirtableInstance } from "@/services/common/airtable";

export async function POST(req) {
const { baseId, tableName, query } = request.body;

const finalQuery = query ? JSON.parse(query) : {};

const records = await AirtableInstance.base(baseId)
.table(tableName)
.select(finalQuery)
.all();

const formattedRecords = records.map((record) => ({
id: record.id,
fields: record.fields,
}));

return NextResponse.json(formattedRecords);
}

this is my root file and I have not exported anything else so this is weird.

worthy axleBOT
#

Hi @pallid rose. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the knowledge sources, I can help explain the error you're encountering and suggest a potential solution.

The error "a 'use server' file can only export async functions, found object" is related to the usage of the "use server" directive in Next.js 14 with the App Router. This issue was discussed in a GitHub issue A "use server" file can only export async functions' error when exporting non-async/cache functions (https://github.com/vercel/next.js/issues/62926).

The problem arises because Next.js is performing a stricter check on files with the "use server" directive. It expects all exports from such files to be async functions.

In your case, you have a POST function that is indeed async, but you're also importing and potentially re-exporting NextResponse and AirtableInstance. These non-async exports might be causing the error.

To resolve this issue, you can try the following:

  1. Remove the "use server" directive from the top of your file. Route handlers in Next.js are server-side by default, so you don't need to explicitly mark them with "use server".

  2. If you need to use "use server" for other reasons, you can try moving your imports outside of the file and only export the async POST function.

Here's how you could modify your code:

import { NextResponse } from "next/server";
import { AirtableInstance } from "@/services/common/airtable";

export async function POST(req) {
  const { baseId, tableName, query } = request.body;

  const finalQuery = query ? JSON.parse(query) : {};

  const records = await AirtableInstance.base(baseId)
    .table(tableName)
    .select(finalQuery)
    .all();

  const formattedRecords = records.map((record) => ({
    id: record.id,
    fields: record.fields,
  }));

  return NextResponse.json(formattedRecords);
}
#

This approach should resolve the error while maintaining the functionality of your route handler.

If you continue to experience issues, you may want to check if there are any other exports in your file that you haven't shown in your question, as the error suggests there might be an object being exported.

For more information on Server Actions and the "use server" directive in Next.js, you can refer to the Next.js documentation on Server Actions and Mutations (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#convention).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@pallid rose Kapa.ai is still learning and improving, please let me know how I did by reacting below.

pallid rose
#

@worthy axle i have removed it but the error persists

worthy axleBOT
#

Let me check the knowledge base for you again. Give me one moment! :books:

#

I'm sorry to hear that the issue persists. The error message suggests that there might be an object being exported from your file.

Please ensure that there are no other exports in your file that are not async functions. This includes any named or default exports.

If you're certain that there are no other exports, there might be an issue with the way your project is set up or with the specific versions of Next.js or other dependencies you're using.

You might want to consider creating a minimal reproduction of your issue and reporting it on the Next.js GitHub (https://github.com/vercel/next.js/issues) to get help from the community.

Also, you can refer to the Next.js documentation (https://nextjs.org/docs) or the Next.js GitHub issues (https://github.com/vercel/next.js/issues) for similar problems and their solutions.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: