简体   繁体   English

ServerError: Invalid JSON 响应正文...在 NextJS 应用程序中提取

[英]ServerError: Invalid JSON response body... in NextJS app with fetch

I have a nextJS app running locally, I also have a flask api running locally.我有一个在本地运行的 nextJS 应用程序,我还有一个在本地运行的 flask api。 I know the flask is returning proper json through Postman.我知道 flask 通过 Postman 返回正确的 json。 The results from the get request to that are below:获取请求的结果如下:

{
    "results": [
        [
            {
                "date_posted": "Mon, 17 Jan 2022 00:00:00 GMT",
                "description": "This is a description",
                "id": 1,
                "mainImg": "https://i.ibb.co/pwxkyGw/How-the-Web-Works-1.png",
                "slug": "Test-Post",
                "subtitle": "Test Subtitle",
                "tags": "test tag",
                "title": "Test Post 2"
            }
        ]
    ]
}

So, now that I knew that was correct I started looking into the javascript:所以,既然我知道这是正确的,我就开始研究 javascript:

import React from 'react';

export const getStaticPaths = async () => {
    const res = await fetch('http://localhost:3000/search/all')
    const data = await res.json()

    const paths = data.results[0].map(post => {
        return {
            params: { id: post.id.toString() }
        }
    })

    return {
        paths,
        fallback: false
    }
}

export const getStaticProps = async (context) => {
    const id = context.params.id
    const res = await fetch('http://localhost:5000/search/postID/' + id)
    const data = await res.json()

    return {
        props: {data}
    }
}

export default function Post({data}) {
  return (
  <div>
      {props.data}
  </div>
    )
}

I checked everything, and for some reason I still get this error:我检查了所有内容,但由于某种原因,我仍然收到此错误:

Server Error
Error: invalid json response body at http://localhost:3000/search/all reason: Unexpected token < in JSON at position 0

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
pages\post\[pid].js (5:17) @ async getStaticPaths

  3 | export const getStaticPaths = async () => {
  4 |     const res = await fetch('http://localhost:3000/search/all')
> 5 |     const data = await res.json()
    |                 ^
  6 | 
  7 |     const paths = data.results[0].map(post => {
  8 |         return {
Show collapsed frames

I understand I am missing something, which is why I am here.我知道我错过了一些东西,这就是我在这里的原因。 If anyone has seen this and knows a fix please let me know!如果有人看到这个并知道解决方法,请告诉我!

So... if you look at the code for both fetches.所以...如果您查看两个提取的代码。 One says port 3000 and the other says 5000... They are both supposed to be 5000. I can not believe it took me this long to catch that smdh.一个说端口 3000,另一个说 5000……它们都应该是 5000。我不敢相信我花了这么长时间才抓住那个 smdh。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点获取帮助 - 获取 FetchError: invalid json response &gt; body at<url> 原因: &gt; JSON 输入意外结束</url> - node-fetch help - getting FetchError: invalid json response > body at <url> reason: > Unexpected end of JSON input UnhandledPromiseRejectionWarning: FetchError: invalid json 响应正文 - UnhandledPromiseRejectionWarning: FetchError: invalid json response body at 有效地使用节点获取,获取无效的 json 响应正文错误*仅*我第一次查询服务器时 - Using node-fetch effectively, getting invalid json response body error *only* the first time I query the server Node FetchError:无效的JSON响应正文-JSON中的意外令牌&lt; - Node FetchError: invalid json response body - unexpected token < in JSON 批量调用API,某些响应返回无效的json响应主体 - Batch call to API some responses are returning invalid json response body JavaScript fetch - 无法在“响应”上执行“json”:正文流已锁定 - JavaScript fetch - Failed to execute 'json' on 'Response': body stream is locked 如何通过then()将Fetch响应的JSON主体传递给Throw Error()? - How can I pass JSON body of Fetch response to Throw Error() with then()? NextJs + Nodemailer + Fetch。 如何接收回复 - NextJs + Nodemailer + Fetch. How to receive a response fetch 给出一个空的响应主体 - fetch gives an empty response body 忽略JavaScript提取响应中的正文 - Ignoring body in a JavaScript fetch response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM