http:// localhost:3000/api/组织原因:意外令牌<在位置0的JSON中

发布于 2025-02-09 11:15:14 字数 1106 浏览 1 评论 0原文

我正在使用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;

This is the error
enter image description here

making request to http://localhost:3000/api/organizations/ from browser returns me expected data but calling it from server side returns above error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-02-16 11:15:14

因为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文