简体   繁体   English

NextJS:TypeError:无法读取未定义的属性“json”

[英]NextJS: TypeError: Cannot read property 'json' of undefined

I've this code into pages folder on my NextJS environment.我将此代码放入我的 NextJS 环境中的 pages 文件夹中。 It gets data calling an external API Rest, and it's working because the console.log(response);它获取调用外部 API Rest 的数据,并且它正在工作,因为console.log(response); line show me by console the Json API response.行通过控制台显示 Json API 响应。 The problem I've is that I get this error in browser:我遇到的问题是我在浏览器中收到此错误:

TypeError: Cannot read property 'json' of undefined类型错误:无法读取未定义的属性“json”

Corresponding with this line code:对应这一行代码:

const data = await res.json();

This is the complete file with the code:这是包含代码的完整文件:

import React from "react";
import fetch from "node-fetch";

const getFetch = async (invoicesUrl, params) => {
  fetch(invoicesUrl, params)
    .then((response) => {
      return response.json();
    })
    .then((response) => {
      console.log(response);
    })
    .catch((err) => {
      console.log(err);
    });
};

export const getServerSideProps = async () => {
  const invoicesUrl = "https://192.168.1.38/accounts/123456";
  const params = {
    method: "get",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
  };
  const res = await getFetch(invoicesUrl, params);
  const data = await res.json();
  console.log("Data Json: ", data);

  return { props: { data } };
};

This is the Json API response that I see by console:这是我在控制台看到的 Json API 响应:

{
  account: [
    {
      id: '7051321',
      type: 'probe',
      status: 'open',
      newAccount: [Object],
      lastDate: '2020-07-04',
      taxExcluded: [Object],
      totalRecover: [Object],
      documentLinks: []
    },
  ]
}

Any idea how can I solve it?知道我该如何解决吗? Thanks in advance.提前致谢。

UPDATE Here the code working good:更新这里的代码运行良好:

import React from "react";
import fetch from "node-fetch";

const getFetch = async (invoicesUrl, params) => {
   return fetch(invoicesUrl, params);
};

export const getServerSideProps = async () => {
  const invoicesUrl = "https://192.168.1.38/accounts/123456";
  const params = {
    method: "get",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
  };
  try {
    const res = await getFetch(invoicesUrl, params);
    const data = await res.json();
    console.log("Data JSON: ", data);
    return { props: { data } };
  } catch (error) {
    console.log("Data ERROR: ", error);
  }
};

There are a couple of things you have to change.有几件事你必须改变。

const getFetch = async (invoicesUrl, params) => {
  fetch(invoicesUrl, params)
    .then((response) => {
      return response.json();
    })
    .then((response) => {
      console.log(response);
      return response; // 1. Add this line. You need to return the response.
    })
    .catch((err) => {
      console.log(err);
    });
};

export const getServerSideProps = async () => {
  const invoicesUrl = "https://192.168.1.38/accounts/123456";
  const params = {
    method: "get",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
  };
  const data = await getFetch(invoicesUrl, params);
  // const data = await res.json(); 2. Remove this you have already converted to JSON by calling .json in getFetch
  console.log("Data Json: ", data); // Make sure this prints the data.

  return { props: { data } };
};

You have return statement in wrong place.你在错误的地方有退货声明。 When the function is expecting a return.当 function 期待回报时。 You need to return when the statements are executed not inside the promise then function because it is an async callback function which is not sync with the statement inside getFetch function. I hope i have made things clear.当语句不在 promise 内执行时,您需要返回then ,因为它是一个异步回调 function,它与getFetch function 内的语句不同步。我希望我已经说清楚了。 Below is the code which will any how return something以下是将如何返回某些内容的代码

import React from "react";
import fetch from "node-fetch";

const getFetch = async (invoicesUrl, params) => {
  return fetch(invoicesUrl, params);
};

export const getServerSideProps = async () => {
  const invoicesUrl = "https://192.168.1.38/accounts/123456";
  const params = {
    method: "get",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
  };
  try{
     const res = await getFetch(invoicesUrl, params);
     console.log("Data Json: ", res);
  }catch(error){
     console.log("Data Json: ", error);
  }
  return { props: { res } };
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM