[英]Internal Next.js API call returns 500 Error
For the life of me, I cannot figure out why an internal API call (Next.js) that I am making keeps returning the dreaded 500 error.对于我的一生,我无法弄清楚为什么我正在进行的内部 API 调用 (Next.js) 不断返回可怕的 500 错误。 The goal here is to make a call to the internal API (Next.js) and then make a call to a Flask server in the backend to retrieve some data (documentation).这里的目标是调用内部 API (Next.js),然后调用后端的 Flask 服务器以检索一些数据(文档)。
From what I can tell, the call in pages/documentation.js
does not execute any request within api/documentation.js
.据我所知, pages/documentation.js
中的调用不会执行api/documentation.js
中的任何请求。 I have logging setup within the Flask server to annotate any requests that are made.我在 Flask 服务器中设置了日志记录,以注释发出的任何请求。
In the frontend, I am seeing Unhandled Runtime Error - SyntaxError: The string did not match the expected pattern.
在前端,我看到Unhandled Runtime Error - SyntaxError: The string did not match the expected pattern.
. . And when inspecting the console I see 500 Internal Server Error - http://localhost:3000/api/documentation
&& Unhandled Promise Rejection: SyntaxError: The string did not match the expected pattern.
在检查控制台时,我看到500 Internal Server Error - http://localhost:3000/api/documentation
&& Unhandled Promise Rejection: SyntaxError: The string did not match the expected pattern.
I am stuck at the moment.我现在被困住了。 Not sure what else I can google to look deeper into this issue.不知道我还能用谷歌搜索什么来更深入地研究这个问题。 My lack of web development experience is biting me here.我缺乏 web 开发经验在这里咬我。 Any help is greatly appreciated.任何帮助是极大的赞赏。
/* pages/documentation.js */
// State Variables
const [documentation, setDocumentation] = useState(null);
useEffect(() => {
if (!documentation) {
fetchDocumentation();
}
}, []);
// API Call to fetch documentation
async function fetchDocumentation() {
const fetchResponse = await fetch('/api/documentation', {
method: 'get',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json'
}
});
if (!fetchResponse.ok) {
setDocumentation(await fetchResponse.text());
} else {
setDocumentation(await fetchResponse.json());
}
};
/* api/documentation.js */
import fetch from 'isomorphic-unfetch'
import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig();
const BACKEND_API_URL = publicRuntimeConfig.BACKEND_API_URL;
export default async function documentationHandler(req, res) {
if (req.method === 'GET') {
let reqURL = BACKEND_API_URL + '/api/documentation';
const backendResponse = await fetch(reqURL, {
mode: 'cors',
method: 'get',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json'
}
});
if (!backendResponse.ok) {
return res.status(backendResponse.status).send(await backendResponse.text());
} else {
const resp = await backendResponse.json();
res.status(200).send(resp);
}
} else {
res.status(403).send('Forbidden');
}
}
I went through your codes... Change the following and give me a feed back...我浏览了您的代码...更改以下内容并给我反馈...
Change:Content-type': 'application/json', To: 'content-Type': 'application/json'更改:内容类型:“应用程序/json”,更改为:“内容类型”:“应用程序/json”
And also Inside of your fetch() @ pages/document.js Set the Method to 'Get' instead of 'get'并且还在你的 fetch() @ pages/document.js 内部将方法设置为 'Get' 而不是 'get'
Since in your api/document.js You stated if(req.method === 'Get')因为在你的 api/document.js 你说 if(req.method === 'Get')
If its a Capital G then make the fetch too same... i wish i was in your codes but well do it and do inform please..如果它是大写字母 G,那么取值太相同了...我希望我在您的代码中,但请做好并告知..
This might seem silly but try and give me feed please这可能看起来很傻,但请尝试给我喂食
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.