Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 无法读取未定义的属性_Reactjs - Fatal编程技术网

Reactjs 无法读取未定义的属性

Reactjs 无法读取未定义的属性,reactjs,Reactjs,我想在不刷新页面的情况下加载一些数据。为此,我对数据进行了民意调查。然而,在这样做时,我得到了控制台错误“无法读取未定义的属性”,并且得到了我期望的输出。我仍然想从控制台中消除这个错误。我理解它抛出了这个错误,因为最初没有返回任何数据。但是有人能帮我解决这个问题吗 下面是代码 componentDidMount() { this.start_polling(); } componentWillUnMount() { this.stop_polling(); } start_p

我想在不刷新页面的情况下加载一些数据。为此,我对数据进行了民意调查。然而,在这样做时,我得到了控制台错误“无法读取未定义的属性”,并且得到了我期望的输出。我仍然想从控制台中消除这个错误。我理解它抛出了这个错误,因为最初没有返回任何数据。但是有人能帮我解决这个问题吗

下面是代码

componentDidMount() {
    this.start_polling();
}

componentWillUnMount() {
    this.stop_polling();
}

start_polling = () => {
    if (!this.is_polling) {
        this.is_polling = true;
        this.poll();
    }
 };

 poll = () => {
     if (this.is_polling) {
         this.poll_timeout = setTimeout(() => {
             this.load_data()
                 .then(this.poll)
                 .catch(this.stop_polling);
         }, poll_interval_ms);
     }
 };

 stop_polling = () => {
    clearTimeout(this.poll_timeout);
    this.is_polling = false;
 };

 load_data = () => {
    const file_name = 'file.json';
    client.get_data_file(this.props.id, file_name, 'json')
        .then((request) => {
            const data = component_name.filter_data(request.response);
            const sorted_data = component_name.sort(data);
            this.data = sorted_data;
            this.setState({final_dat: [this.data]});
        })
        .catch((error) => {
          //something
        });
};

问题是load_数据没有返回任何内容,但您希望它返回一个承诺,并对返回的值执行
.then()
。只需添加加载返回数据,即可设置:

load_data = () => {
    const file_name = 'file.json';
    return client.get_data_file(this.props.id, file_name, 'json')
        .then((request) => {
            const data = component_name.filter_data(request.response);
            const sorted_data = component_name.sort(data);
            this.data = sorted_data;
            this.setState({final_dat: [this.data]});
        })
        .catch((error) => {
          //something
        });
};

当然,请确保
client.get_data_file
也正确返回承诺。

load_data函数返回什么?如果不使用setTimeout()执行此操作?Ie就是这样。load_data()。然后(…Tzelon:更新代码以包含load_数据method@rrd:谢谢您尝试这样做。不起作用。离题警告:您对带下划线的命名函数的习惯不好。请帮自己一个忙,开始使用camelCase