获取Prototype.json()会损坏/更改正文数据

时间:2019-02-20 08:13:00

标签: javascript json fetch

我已经撞了一个星期了。这是不可能的,但它确实发生了(我知道是javascript):

我正在使用fetch从服务器获取数据,如下所示:

fetch(this.props.url, {
            method: 'POST',
            body: JSON.stringify({
                class: 'property',
                action: 'view', 
                objectId: this.props.element.id,
                token: this.props.token,
            }),
        })
            .then(response => response.json())
            .then(json => {
                console.log(json);})

现在,用Postman进行测试可以得到正确的结果:

"units": [
            {
                "amount_due": 22.0,
                "id": 31,
                "floor": 5,
                "common_percentage": 0.012223,
                "building": {} 
            },
            {
                "amount_due": 16.0,
                "id": 33,
                "floor": 5,
                "common_percentage": 0.012143223,
                "building": {} 
            }
         ]

但是,控制台会记录两个ID为33的设备。

检查后端100次,并返回正确的数据。邮差确认。还尝试使用.text()而不是.json()解析响应,并且.text()返回正确的数据。此损坏仅发生在.json()中。我会发疯还是通过.json()方法发现错误(或特殊之处)?

谢谢。

编辑1:

由于@ChrisG,我发现我的代码在某种程度上破坏了数据。这一切都在this.setState被调用之后发生:

let completedProjects = json.property.projects.filter((project) => project.finish_date);
                    let completedJobs = json.property.jobs.filter((job) => job.complete.filter((completion) => completion.id == this.props.user.id).length > 0 && !job.on_hold && !job.deleted);
                    let timeline = [{title: <Trans>heading.property.added</Trans>, createdAt: <Moment locale={this.props.i18n.language} format="ll">{json.date_created}</Moment>, comment: <Trans>description.property.added</Trans>, icon: <FontAwesomeIcon icon="home" />, sortCriterion: this.props.element.date_created }];
                    let inspections = this.state.inspections; 
                    json.inspections.map((inspection)=>{
                        inspections.push({description: inspection.type.description, date: <Moment format="ll">{inspection.instance.date_performed}</Moment>, name: inspection.type.name, interval: inspection.type.interval, typeId: inspection.type.id, });
                    });
                    completedProjects.map((project) => {
                        timeline.push({title: <Trans>heading.project.completed</Trans>, createdAt: <Moment format="ll">{project.finish_date}</Moment>, comment: project.name, icon: <FontAwesomeIcon icon="project-diagram" />, sortCriterion: project.finish_date, iconColor: '#007bff' });
                    });
                    completedJobs.map((job) => {
                        timeline.push({title: <Trans>heading.job.finished</Trans>, createdAt: <Moment format="ll">{job.finish_date}</Moment>, comment: job.name, icon: <FontAwesomeIcon icon="check" />, sortCriterion: job.finish_date, iconColor: '#007bff' });
                    });
                    timeline.sort((a, b) => a.sortCriterion < b.sortCriterion);
                    this.setState({
                        element: json.property,
                        activeJobs: json.property.jobs.filter((job) => job.complete.filter((completion) => completion.id == this.props.user.id).length == 0 && !job.on_hold && !job.deleted),
                        completedJobs: completedJobs,
                        remainingJobs: json.property.jobs.filter((job) => job.on_hold || job.deleted),
                        // activeProjects: json.property.projects.filter((project) => !project.finish_date),
                        activeProjects: json.property.projects,
                        completedProjects: completedProjects,
                        collapse: true,
                        timeline: timeline,
                    });

一个澄清:“单元”部分(已损坏的部分)是“属性”对象的属性。现在,我正在写这篇文章,也许“属性”是某种保留字,会把事情弄糟吗?

编辑2:

将问题归结为:

this.setState({element: json.property})

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,事实证明问题出在this.setState()函数之内,因为它以某种方式破坏了数据。因此,提取response.proptotype.json()会按预期运行。我正在打开一个新期刊来跟踪实际问题。感谢@ChrisG的指导。