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:
-
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".
-
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);
}