【问题标题】:React Js Uncaught (in promise) TypeError: Cannot read property of undefinedReact Js Uncaught (in promise) TypeError: Cannot read property of undefined
【发布时间】:2018-05-11 03:59:29
【问题描述】:

我正在学习做出反应,在发出 ajax 请求时面临这个问题,即使我得到了响应但无法将该响应传递给其他函数 { this.showBattersData(data);我可以在哪里渲染 dom。这很明显,我在这里遗漏了一些东西。任何帮助将不胜感激。

import React, {Component} from 'react'
import { fetchFactory } from '../Fetch/fetchFactory'
    
    var batters = [];
    var rowdata = "";

    
export class Body extends React.Component {
    constructor(props) {
        super(props);
        this.battersDataFetchReq = this.battersDataFetchReq.bind(this);
        this.showBattersData = this.showBattersData.bind(this);
        this.battersDataFetchReq();
    }    

    //ajax request
    battersDataFetchReq() {
        fetchFactory('./assets/some.json', 'GET')
        .then(function(data) {
            //this.showBattersData = this.showBattersData.bind(this);
            this.showBattersData(data); // this is where i'm getting this error

        })
    }

    showBattersData(res) {
        console.log("inside showBattersData:: ", res);
        rowdata = `
            <table id="batters" name="batters" class="table table-bordered table-hover">
            <caption>List of batters</caption>
            <th>#</th>
            <th>Id</th>
            <th>Type</th>
            <tbody id="batters_body">`;    
        batters = res.batters.batter;
        for(var i = 0; i < batters.length; i++) {
            rowdata += `
                <tr>
                    <td>${[i + 1]}</td>
                    <td>${batters[i].id}</td>
                    <td>${batters[i].type}</td>
                </tr>`;
        }
        rowdata += `</tbody></table>`;
    }


    render() {
        return (
            <div>
                {rowdata}
            </div>
        );
    }
}

【问题讨论】:

标签: javascript reactjs typeerror


【解决方案1】:

您的问题与JavaScript 中的Context 概念有关。

在 JS 中,functions 有它们的上下文,这意味着 this 关键字指的是函数本身。

您可以使用以下两种解决方案之一:

一个:

battersDataFetchReq() {
    let self = this;

    fetchFactory('./assets/some.json', 'GET')
        .then(function (data) {            
            self.showBattersData(data);    
        })
}

Tow(又名Arrow function):推荐的解决方案

battersDataFetchReq() {
    fetchFactory('./assets/some.json', 'GET')
        .then((data) => {
            this.showBattersData(data);
        })
}

想知道为什么?来自 MDN:

箭头函数表达式的语法比函数表达式短,并且没有自己的 this。

继续阅读...(两个链接都来自MDN

【讨论】:

  • 谢谢,@Mehdi Dehghani 抽出时间,箭头函数工作正常,唯一的问题是在 render() 函数内部,行数据显示为空白,但是当从 showBattersData() 控制台时,它拥有所有数据,任何解决方案。
  • React 在状态改变时重新渲染,所以也许你应该尝试在 ajax req 完成时改变状态。见stackoverflow.com/q/39066972/3367974reactjs.org/docs/faq-ajax.html
【解决方案2】:

您需要.bind(this)您的请求回调或使用箭头函数:

//ajax request
battersDataFetchReq() {
    fetchFactory('./assets/some.json', 'GET')
    .then((data) => { // Here, use a lambda or this will not point to your class
        this.showBattersData(data);
    })
}

【讨论】:

  • 你是说箭头函数 () => ?
猜你喜欢
  • 2019-02-12
  • 2021-09-20
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
相关资源
最近更新 更多