我正在尝试在一个新的nextjs 13项目中进行提取(我不确定这是js还是nextjs的问题),并使用console.logs来观察代码的执行。
let url = `${BASE}/${module}/${path}`;
url += "?" + new URLSearchParams(params).toString();
console.log(`${url}`);
let response = await fetch(`${url}`, {
method: "GET",
headers: {
"expect-cache-tag": API_KEY,
Authorization:
"Bearer ...",
},
});
console.log(4);
const text = await response.text();
console.log(5);
try {
console.log(6);
const json = JSON.parse(text);
console.log(7);
return json;
} catch (err) {
throw new Error("Did not receive JSON, instead received: " + text);
}
} catch (err: any) {
console.log(8);
let response = err.response;
return response;
}
对于某些请求,它运行得很好,但对于其他请求,我可以看到它被日志卡住了:
1
2
https://...
4
因此,它似乎在解析text()时遇到了麻烦,如果我使用json(),它也会遇到同样的问题。
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://...',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: undefined
}
}
感谢您的宝贵意见
2条答案
按热度按时间brqmpdu11#
在我的例子中,它只发生在我尝试从中间件的本地获取api时,然后我尝试使用路由api https://nextjs.org/docs/api-routes/introduction Package api,它工作了,不知道为什么
r6hnlfcb2#
似乎这是一个已知的问题,并谈到在next.js GitHub:https://github.com/vercel/next.js/issues/41853