admin管理员组文章数量:1178594
I'm sending a value to my server side code that isn't being read correctly. I'm using the NextJS experimental App directory
//src/app/api/auth/route.js
export async function POST(req, res) {
console.log(req.body);
const { address } = req.body;
const isAuthenticated = await checkBalance(address, threshold);
if (isAuthenticated) {
return new Response("Authorized", { status: 200 });
} else if (isAuthenticated == false) {
return new Response("Unauthorized", { status: 401 });
} else if (isAuthenticated == undefined) {
return new Response("Error", { status: 500 });
}
}
console log is: ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
const address is undefined
.
This is the api call:
const response = await fetch("/api/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ address: walletAddress }),
});
const data = await response.json();
I read in another answer to a similar question that nextjs12+ is supposed to parse the request automatically- what am I doing wrong? I'm assuming that nextjs has a protocol in place for decoding the ReadableStream but I can't find anything in the docs or examples for this, maybe because there is a framework agnostic method for decoding the object that is unknown to me?
Thank you in advance.
I'm sending a value to my server side code that isn't being read correctly. I'm using the NextJS experimental App directory
//src/app/api/auth/route.js
export async function POST(req, res) {
console.log(req.body);
const { address } = req.body;
const isAuthenticated = await checkBalance(address, threshold);
if (isAuthenticated) {
return new Response("Authorized", { status: 200 });
} else if (isAuthenticated == false) {
return new Response("Unauthorized", { status: 401 });
} else if (isAuthenticated == undefined) {
return new Response("Error", { status: 500 });
}
}
console log is: ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
const address is undefined
.
This is the api call:
const response = await fetch("/api/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ address: walletAddress }),
});
const data = await response.json();
I read in another answer to a similar question that nextjs12+ is supposed to parse the request automatically- what am I doing wrong? I'm assuming that nextjs has a protocol in place for decoding the ReadableStream but I can't find anything in the docs or examples for this, maybe because there is a framework agnostic method for decoding the object that is unknown to me?
Thank you in advance.
Share Improve this question edited Mar 31, 2023 at 15:08 Bigboss01 asked Mar 31, 2023 at 14:00 Bigboss01Bigboss01 6181 gold badge7 silver badges26 bronze badges 5 |4 Answers
Reset to default 31I've faced a similiar problem, you can try this:
const body = await req.json()
const {address} = body
// the rest of your code
you need to await the request from nextjs in order to get the body object. Like the code bellow:
import { NextResponse } from "next/server";
export async function POST(req) {
const body = await req.json();
console.log(body);
return NextResponse.json(body);
}
A discord user recommended const address = await req.json();
instead of const {address} = req.body
and it works.
I think what you want to do is:
...
const { address } = req.body
const isAuthenticated = await checkBalance(address, threshold);
...
本文标签: javascriptDecode ReadableStream object nextjs 13 api routeStack Overflow
版权声明:本文标题:javascript - Decode ReadableStream object nextjs 13 api route - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738057918a2056592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const { address } = JSON.parse(req.body);
– inkredusk Commented Mar 31, 2023 at 14:13SyntaxError: Unexpected token 'o', "[object Rea"... is not valid JSON
– Bigboss01 Commented Mar 31, 2023 at 15:14NexApiRequest
instead ofNextRequest
. – ReZ Commented May 9, 2023 at 13:30