【发布时间】:2021-07-23 01:08:27
【问题描述】:
这是我遇到的错误。
```
**EventList.js:70 Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'FiberNode'
| property 'stateNode' -> object with constructor 'HTMLInputElement'
--- property '__reactInternalInstance$rpowr4rh70f' closes the circle
at JSON.stringify (<anonymous>)
at Object.addEvent (EventList.js:70)
at handleSave (AddEvent.js:34)
at HTMLUnknownElement.callCallback (react-dom.development.js:336)**
```
-
看起来问题出在 EventList 第 70 行和 AddEvent 行 34 在这里。
事件列表第 70 行
``` //Add New Event addEvent(event) { const proxyurl = " https://secret-ocean-49799.herokuapp.com/"; const url = SERVER_URL + '/api/v1/event'; fetch(proxyurl + url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(event) {/*<-- Line 70*/} }) .then(res => this.fetchEvents()) .catch(err => console.error(err))}
AddEvent line 34
import React, { useState } from 'react';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import Button from '@material-ui/core/Button';
const AddEvent = (props) => {
const [open, setOpen] = useState(false);
const [event, setEvent] = useState({
id: '',
title: '',
subTitle: '',
startDate: '',
displayUntilDate: '',
location: '',
description: '',
infoLink: ''
});
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
const handleChange = (event) => {
setEvent({ ...event, [event.target.name]: event.target.value });
}
const handleSave = () => {
props.addEvent(event); {/*<-- Line 34 for Add Event*/}
handleClose();
}
return (
<div>
<br />
<button class="" variant="outlined" color="primary" onClick={handleClickOpen}
>Add New Event
</button>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>New Event</DialogTitle>
<DialogContent>
<input type="text" placeholder="Id" name="id"
value={event.id} onChange={handleChange} /><br />
<input placeholder="Title" name="title"
value={event.title} onChange={handleChange} /><br />
<input type="text" placeholder="Sub Title" name="subTitle"
value={event.subTitle} onChange={handleChange} /><br />
<input type="date" placeholder="Start Date" name="startDate"
value={event.startDate} onChange={handleChange} /><br />
<input type="date" placeholder="Display Until Date" name="displayUntilDate"
value={event.displayUntilDate} onChange={handleChange} /><br />
<input type="text" placeholder="Location" name="location"
value={event.location} onChange={handleChange} /><br />
<input type="text" placeholder="Description" name="description"
value={event.description} onChange={handleChange} /><br />
<input type="text" placeholder="Info Link" name="infoLink"
value={event.infoLink} onChange={handleChange} /><br />
</DialogContent>
<DialogActions>
<button onClick={handleClose}>Cancel</button>
<button onClick={handleSave}>Save</button>
</DialogActions>
</Dialog>
</div>
);
};
export default AddEvent;
```
据我了解,当您有一个引用其他父对象的对象并且如果 JSON.stringify 打印了循环数据,则字符串将是无穷大时,就会发生循环数据。
所以,看起来是 Stringify 函数导致了问题,但我看不出它是如何引用父对象的。
起初我虽然是因为包含了 id 值,但是随着新对象的添加也会自动序列化。我删除了与 Id 信息交互的代码部分,但是没有用。
【问题讨论】:
标签: json reactjs web typeerror stringify