简体   繁体   English

未处理的拒绝(TypeError):无法读取未定义的属性(读取“状态”)

[英]Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'state')

I tried to pass the state value using onSubmit function but it returns an Unhandled error which i couldn't find what the error is Kindly help me with this我尝试使用 onSubmit function 传递 state 值,但它返回一个未处理的错误,我找不到错误是什么请帮助我

React Code:反应代码:

import React, { Component } from 'react'
import 'bootstrap/dist/css/bootstrap.css';
import { DateRangePicker } from 'rsuite';
import moment from 'moment'
export default class DateComponent extends Component {
  constructor() {
    super();
    this.state = {
      datefilter: [],
      startDate: "",
      endDate: ""

    }
    this.handleChange = this.handleChange.bind(this);
  
  }

  handleChange(start) {
   
    console.log()
    this.setState({
      startDate: start[0],
      endDate: start[1]
    })
    
  }

  async componentDidMount() {
    
    if (this.state.startDate == '') {
   
      console.log(Object.keys(moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss')).length)
      const response = await fetch('http://localhost:8081/items');
      const data = await response.json();
     
      this.setState({ datefilter: data })
    }

  }

  async onFormSubmit()  {
    
  var start = moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss');
  var end = moment(this.state.endDate).format('yyyy-MM-DDTHH:mm:ss');
 console.log(Object.keys(moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss')).length)
 console.log(start,end)
  var startDate = new Date(start).getTime();
  var endDate = new Date(end).getTime();
  const response = await fetch('http://localhost:8081/items');
  const data = await response.json();
  var result = data.filter(d => {
    var time = new Date(d.Date).getTime();
    return (startDate < time && time < endDate);
  });
  console.log(result);
  this.setState({ datefilter: result })

 }
  render() {
    const datefilter = this.state.datefilter
    return (
      <div>
        <form onSubmit={this.onFormSubmit}> 
        <DateRangePicker
         format="yyyy-MM-dd HH:mm:ss"
          onChange={this.handleChange} 
          onOk={this.onFormSubmit}
          placeholder="Enter Date and Time" 
          />
        </form>
    
 
        <table class="table">
          <thead class="thead-dark">
            <tr>
              <th scope="col">Item ID</th>
              <th scope="col">Item Name</th>
              <th scope="col">Status</th>
              <th scope="col">Date</th>
            </tr>
          </thead>
          <tbody>
            {datefilter.map(table => (
              <tr>
                <td>{table.ID}</td>
                <td>{table.ItemName}</td>
                <td>{table.Status}</td>
                <td>{table.Date}</td>
              </tr>
            ))}
          </tbody>
        </table>
       
      
      </div>
    )
  }
}

the above code is the table filter code if the state is null it returns all if it has value then it returns onFormSubmit function.上面的代码是表格过滤器代码,如果 state 是 null 它返回所有如果它有值然后它返回 onFormSubmit function。 I tried to a lot but couldn't find out since im new to react.我尝试了很多,但由于我是新来的反应而无法找到。 i need this community to help me我需要这个社区来帮助我

Also new to JS.也是 JS 的新手。 So, please take below with caution.因此,请谨慎使用以下内容。

async onFormSubmit()  {
    
  var start = moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss');
  var end = moment(this.state.endDate).format('yyyy-MM-DDTHH:mm:ss');
 console.log(Object.keys(moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss')).length)
 console.log(start,end)

Would you please try following steps:请您尝试以下步骤:

  1. Hard-code the 'start' and 'end' to some time-stamp.将“开始”和“结束”硬编码到某个时间戳。
  2. And see if that error persists.并查看该错误是否仍然存在。

Why?为什么? The error, as it reads:错误如下:

"... Cannot read properties of undefined reading ('state')" “...无法读取未定义读取的属性('state')”

  • indicates that there must be some form of an object (may be "this.state") where表示必须有某种形式的 object(可能是“this.state”),其中

state state

is expected to be a property, but that object is simply not defined.预计是一个属性,但 object 根本没有定义。

If I were you, I will try to add below line如果我是你,我会尝试在下面添加

this.onFormSubmit = this.onFormSubmit.bind(this)

just below略低于

this.handleChange = this.handleChange.bind(this);

on the constructor method.关于构造方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):无法读取未定义的属性(读取“哈希”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'hash') 可能未处理的拒绝 [3] 类型错误:无法读取未定义的属性(读取“原型”) - Potentially unhandled rejection [3] TypeError: Cannot read properties of undefined (reading 'prototype') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“数据”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'data') 反应:未处理的拒绝(TypeError):无法读取未定义的属性(读取“错误”) - React : Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“参数”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'params') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“值”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value') 未处理的拒绝(TypeError):无法读取我的 carActions.js 中未定义(读取“数据”)的属性 - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'data') in my cartActions.js 未处理的拒绝(TypeError):在 React 中使用 useRef 时无法读取未定义的属性(读取“值”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value') when working with useRef in React 未处理的拒绝(TypeError):无法读取未定义的属性(读取“推送”),使用 useHistory - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'push') , using useHistory 未处理的拒绝(TypeError):无法读取 null 的属性(读取“产品”) - Unhandled Rejection (TypeError): Cannot read properties of null (reading 'product')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM