res.setHeader is not a function?
Answered
Champagne D’Argent posted this in #help-forum
Champagne D’ArgentOP
I get this error in my upload function using next14:
This is the api route method:
What is wrong with setHeader?
⨯ TypeError: res.setHeader is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/upload/route.ts:44:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/utdev/code/what_zip/node_modules/.pnpm/next@14.0.1_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:61856This is the api route method:
export async function POST(
req: NextApiRequest,
res: NextApiResponse
) {
const chunks = [];
for await (const chunk of req.body) {
chunks.push(chunk);
}
const body = Buffer.concat(chunks).toString();
console.log("body", body);
const { filename, fileHash, contentType } = JSON.parse(body);
console.log({ filename, fileHash, contentType })
const signedUrl = await getSignedUrl(
R2,
new PutObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: `resources/${fileHash}/${filename}`,
ContentType: contentType as string,
}),
{ expiresIn: 3600 }
);
console.log("signedUrl", signedUrl);
res.setHeader("Access-Control-Allow-Origin", "*");
res.json({
url: signedUrl,
method: "PUT",
});
res.end();
}What is wrong with setHeader?
Answered by riský
you should look at the docs for the new route handler: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#headers (short story is that they changed many apis)
2 Replies
@Champagne D’Argent I get this error in my upload function using next14:
⨯ TypeError: res.setHeader is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/upload/route.ts:44:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/utdev/code/what_zip/node_modules/.pnpm/next@14.0.1_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:61856
This is the api route method:
ts
export async function POST(
req: NextApiRequest,
res: NextApiResponse
) {
const chunks = [];
for await (const chunk of req.body) {
chunks.push(chunk);
}
const body = Buffer.concat(chunks).toString();
console.log("body", body);
const { filename, fileHash, contentType } = JSON.parse(body);
console.log({ filename, fileHash, contentType })
const signedUrl = await getSignedUrl(
R2,
new PutObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: `resources/${fileHash}/${filename}`,
ContentType: contentType as string,
}),
{ expiresIn: 3600 }
);
console.log("signedUrl", signedUrl);
res.setHeader("Access-Control-Allow-Origin", "*");
res.json({
url: signedUrl,
method: "PUT",
});
res.end();
}
What is wrong with setHeader?
you should look at the docs for the new route handler: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#headers (short story is that they changed many apis)
Answer
Champagne D’ArgentOP
ok that makes sense let me check