javascript - React Js Uncaught( promise )TypeError : Cannot read property of undefined

标签 javascript reactjs typeerror

我正在学习使用react,在发出 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 中的Context 概念有关。

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

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

一:

battersDataFetchReq() {
    let self = this;

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

Tow(又名箭头函数):推荐的解决方案

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

想知道为什么吗?来自 MDN:

An arrow function expression has a shorter syntax than a function expression and does not have its own this.

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

关于javascript - React Js Uncaught( promise )TypeError : Cannot read property of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284784/

相关文章:

javascript - TypeError:api.getAll 不是函数,服务方法无法识别

javascript - 在 ReactJS 中使用服务器端 HTML 模板

python - 使用 for 循环迭代并引用 lst[i] 时出现 TypeError/IndexError

javascript - jQuery:排序)表不适用于新版本的 jQuery

javascript - Jquery 数据表初始化时的一次回调

javascript - 如何比较数组中的每个元素并将具有相同数据的元素分组(在我的例子中是日期)? JavaScript

javascript - react : how to force state changes to take place after setState

Python 错误 : List Object Not Callable with For Loop

javascript - Rails 4 Flot 未捕获类型错误和错误显示

javascript - jQuery 位置 DIV 固定在除主页外的滚动顶部