简体   繁体   English

当我尝试从 HomeScreen 打开产品时出现以下错误 - 将循环结构转换为 JSON -->

[英]I am getting the below error when I am trying to open a product from HomeScreen - Converting circular structure to JSON -->

when I click on a product from HomeScreen.js which should take me to the ProductScreen to show product details but I am getting the below error.当我单击 HomeScreen.js 中的产品时,该产品应将我带到 ProductScreen 以显示产品详细信息,但出现以下错误。

Converting circular structure to JSON --> starting at object with constructor 'NativeTopology' |将圆形结构转换为 JSON --> 从构造函数“NativeTopology”的对象开始 | property 's' -> object with constructor 'Object' |属性 's' -> 构造函数为 'Object' 的对象 | property 'sessionPool' -> object with constructor 'ServerSessionPool' --- property 'topology' closes the circle属性 'sessionPool' -> 带有构造函数 'ServerSessionPool' 的对象 --- 属性 'topology' 关闭圆圈

The following is the code I am running以下是我正在运行的代码

ProductConstants.js
export const PRODUCT_DETAILS_REQUEST = "PRODUCT_DETAILS_REQUEST";

export const PRODUCT_DETAILS_SUCCESS = "PRODUCT_DETAILS_SUCCESS";

export const PRODUCT_DETAILS_FAIL = "PRODUCT_DETAILS_FAIL";


productDetailsReducer --Reducer
import {
  PRODUCT_DETAILS_REQUEST,
  PRODUCT_DETAILS_SUCCESS,
  PRODUCT_DETAILS_FAIL,
} from "../constants/productConstants";

export const productDetailsReducer = (
  state = { product: { reviews: [] } },
  action
) => {
  switch (action.type) {
    case PRODUCT_DETAILS_REQUEST:
      return { loading: true, ...state };
    case PRODUCT_DETAILS_SUCCESS:
      return { loading: false, product: action.payload };
    case PRODUCT_DETAILS_FAIL:
      return { loading: false, error: action.payload };
    default:
      return state;
  }
};





 listProductDetails -- Action
import axios from "axios";
import {
  PRODUCT_DETAILS_REQUEST,
  PRODUCT_DETAILS_SUCCESS,
  PRODUCT_DETAILS_FAIL,
} from "../constants/productConstants";

export const listProductDetails = (id) => async (dispatch) => {
  try {
    dispatch({ type: PRODUCT_DETAILS_REQUEST });

    const { data } = await axios.get(`/api/products/${id}`);

    dispatch({
      type: PRODUCT_DETAILS_SUCCESS,
      payload: data,
    });
  } catch (error) {
    dispatch({
      type: PRODUCT_DETAILS_FAIL,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};


ProductScreen.js

import React, { useEffect } from "react";
import { Link } from "react-router-dom";
import { Row, Col, ListGroup, Image, Button, Card } from "react-bootstrap";
import Ratings from "../components/Ratings";
import { useDispatch, useSelector } from "react-redux";
import { listProductDetails } from "../actions/productActions";
import Loader from "../components/loader";
import Message from "../components/message";

const ProductScreen = ({ match }) => {
  const dispatch = useDispatch();

  const productDetails = useSelector((state) => state.productDetails);

  const { loading, error, product } = productDetails;

  useEffect(() => {
    dispatch(listProductDetails(match.params.id));
  }, [dispatch, match]);

  return (
    <>
      <Link className="btn btn-light my-3" to="/">
        Go Back
      </Link>
      {loading ? (
        <Loader />
      ) : error ? (
        <Message>{error}</Message>
      ) : (
        <Row>
          <Col md={6}>
            <Image src={product.image} alt={product.name} fluid />
          </Col>
          <Col md={3}>
            <ListGroup variant="flush">
              <ListGroup.Item>
                <h3>{product.name}</h3>
              </ListGroup.Item>
              <ListGroup.Item>
                <Ratings
                  value={product.rating}
                  text={`${product.numReviews} reviews`}
                />
              </ListGroup.Item>
              <ListGroup.Item>Price: ${product.price}</ListGroup.Item>
              <ListGroup.Item>
                Description: ${product.description}
              </ListGroup.Item>
            </ListGroup>
          </Col>
          <Col md={3}>
            <Card>
              <ListGroup variant="flush">
                <ListGroup.Item>
                  <Col>Price:</Col>
                  <Col>
                    <strong>{product.price}</strong>
                  </Col>
                </ListGroup.Item>

                <ListGroup.Item>
                  <Col>Stock:</Col>
                  <Col>
                    {product.countInStock > 0 ? "In Stock" : "Out of Stock"}
                  </Col>
                </ListGroup.Item>

                <ListGroup.Item>
                  <Button
                    class="btn btn-block"
                    type="button"
                    disabled={product.countInStock === 0}
                  >
                    Add to Cart
                  </Button>
                </ListGroup.Item>
              </ListGroup>
            </Card>
          </Col>
        </Row>
      )}
    </>
  );
};

export default ProductScreen;

The reason why you get this issue is that one of the objects you pass as a second argument to your function has a circular reference(s) to itself or to other objects that point to that object.您遇到此问题的原因是您作为第二个参数传递给函数的对象之一具有对自身或指向该对象的其他对象的循环引用。 If not all of them have circular references, you can wrap the serialization with try/catch to save your program from crashing.如果不是全部都有循环引用,您可以使用 try/catch 包装序列化以防止程序崩溃。

Consequently, we get a runtime error saying we have circular references:因此,我们得到一个运行时错误,说我们有循环引用:

Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'Object' |未捕获的类型错误:将循环结构转换为 JSON --> 从构造函数“Object”的对象开始 | property 'someRef' -> object with constructor 'Object' --- property 'someRef' closes the circle属性 'someRef' -> 带有构造函数 'Object' 的对象 --- 属性 'someRef' 关闭圆圈

That's because JSON can't serialize cyclic data structures by design.那是因为 JSON 在设计上无法序列化循环数据结构。

暂无
暂无

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

相关问题 为什么在运行myqsl查询时在nodeJS中出现“将循环结构转换为JSON”错误? - Why am i getting the 'Converting circular structure to JSON' error in nodeJS while running myqsl query? 我无法通过 nodejs 下面的代码获取数据? 它显示 TypeError: Converting circular structure to JSON - i am not able to get the data throught below code in nodejs ? it shows TypeError: Converting circular structure to JSON 我总是得到 JSON 错误和未处理的承诺拒绝警告的循环结构。 我该如何摆脱它? - I am always getting circular structure to JSON error and unhandled promise rejection warning. How do I get rid of it? 尝试从数据库中获取信息时出现错误 - I am getting an error when trying to get information from the database 为什么我收到错误“试图打开未关闭的连接。”? - Why am I getting error “Trying to open unclosed connection.”? 我正在尝试在 VS 中打开 Cypress,但不断收到此错误消息 - I am trying to open Cypress in VS but keep getting this error message 我正在尝试在 nodejs 中使用来自 DB 的搜索产品,但产品没有得到? - I am trying to use search product from DB, in nodejs but the product is not getting? 尝试运行我的机器人命令时出现此错误 - I am getting this error when trying to run my bot commands 为什么在尝试安装和运行 npm 时出现错误? - Why am I getting an error when trying to install and run npm? 当我尝试运行此代码时,出现以下错误 - When i am trying to run this code i am getting the following error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM