http:// localhost:3000/api/组织原因:意外令牌<在位置0的JSON中
我正在使用getServersideProps来向后端请求数据请求,
export async function getServerSideProps(context) {
const res = await MachineService.listOrgs()
const organizatons = [
'csdc3443dsvk',
'csddscd3443dsvk',
'12dc3443dsvk',
'csdc3443ds56k',
'csdc3443ds67',
]
return {
props: { organizatons }, // will be passed to the page component as props
}
}
这是我的Listorgs代码
const listOrgs = async (nextToken, limit) => {
return new Promise(async (resolve, reject) => {
fetch('http://localhost:3000/api/organizations/')
.then(res => res.json())
.then((result) => {
resolve({ Organizations: result })
})
.catch((error) => reject(error))
});
}
export default listOrgs;
向http:// localhost提出请求:3000/api/abersanations/从浏览器返回我的预期数据,但从服务器端返回上述错误
I am using getServerSideProps to make backend request for data
export async function getServerSideProps(context) {
const res = await MachineService.listOrgs()
const organizatons = [
'csdc3443dsvk',
'csddscd3443dsvk',
'12dc3443dsvk',
'csdc3443ds56k',
'csdc3443ds67',
]
return {
props: { organizatons }, // will be passed to the page component as props
}
}
Here is my listOrgs code
const listOrgs = async (nextToken, limit) => {
return new Promise(async (resolve, reject) => {
fetch('http://localhost:3000/api/organizations/')
.then(res => res.json())
.then((result) => {
resolve({ Organizations: result })
})
.catch((error) => reject(error))
});
}
export default listOrgs;
making request to http://localhost:3000/api/organizations/ from browser returns me expected data but calling it from server side returns above error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 fetch 仅在浏览器(客户端)(客户端) ),
getServersideProps
在服务器端(nodejs)运行,因此您的代码无法正常工作。您可以尝试 axios 或 isomorphic-fetch ,这些方法在服务器和客户端环境上运行Because fetch is only work on browser (client side),
getServerSideProps
run in server side (Nodejs) so your code is not working. You can try axios or isomorphic-fetch, these methods running on both server and client environment