【问题标题】:TypeError: Cannot read property 'setState' of nullTypeError:无法读取 null 的属性“setState”
【发布时间】:2017-05-17 07:49:05
【问题描述】:

我正在尝试将返回参数从 firebase 传递给状态参数,但我收到了这个错误:

App.js:43 Uncaught TypeError: Cannot read property 'setState' of null

我想将“snapshot”数组传递给“calendars”,然后检索“calendars.summary”或“calendars.date”之类的日历值(我知道该怎么做,但问题是我做不到将快照分配给日历)。

componentDidMount() {
    //setInterval(() => start(this.state.gapi,true), 30000);
    var rootRef = firebase.database().ref();
    var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    console.log(child.key+": "+child.child("summary").val());


  });
  this.setState({
      calendars: snapshot
    });
});
    }

我该如何解决?谢谢

更新 在 Mayank Shukla 发表评论后,我可以通过以下方式正确传递“快照”:

componentDidMount() {
    //setInterval(() => start(this.state.gapi,true), 30000);
    var rootRef = firebase.database().ref();
    var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    console.log(child.key+": "+child.child("summary").val());

  });
  this.setState({
      calendars: snapshot
    });
}.bind(this));
    }

componentDidMount() {
    //setInterval(() => start(this.state.gapi,true), 30000);
    var rootRef = firebase.database().ref();
    var urlRef = rootRef.child("testCalendar/calendars");
urlRef.once("value", (snapshot) => {
  snapshot.forEach(function(child) {
    console.log(child.key+": "+child.child("summary").val());

  });
  this.setState({
      calendars: snapshot
    });
});
    }

但现在我有这个问题:

Uncaught TypeError: this.state.calendars.map is not a function

关于这个功能:

_getCalendarsList(){
     var d = new Date();

    return this.state.calendars.map((calend) => {
    return (
      <Calendar 
      {...calend} 
      key={d.getTime()} />

      )
    });
  }

【问题讨论】:

  • 绑定问题,使用箭头函数:urlRef.once("value", (snapshot) =&gt; {解决问题。
  • @MayankShukla 好的,但现在我遇到了这个问题:“TypeError: this.state.calendars.map is not a function”也许是因为快照不是可迭代的??
  • _getCalendarsList(){ var d = new Date(); return this.state.calendars.map((calend) => { return ( ) }); }
  • this.state.calendar 应该是一个数组来映射它,console.log(this.state.calendar) 在函数中,看看你得到了什么对象

标签: javascript reactjs firebase firebase-realtime-database calendar


【解决方案1】:

要在任何变量上使用map,该变量必须是array,您在componentDidMount方法中获取数据,它将在初始渲染后被调用,因此请确保@的初始值987654325@的状态应该是[],像这样:

constructor(){
   super();
   this.state = {
      calendars: [],
      ....
   }
}

或者在使用map之前打勾,如果不是array那么return null,像这样:

_getCalendarsList(){
    if(!Array.isArray(this.state.calendars))
       return null;

    var d = new Date();

    return this.state.calendars.map((calend) => {
        return (
             <Calendar 
                  {...calend} 
                  key={d.getTime()} 
             />

        )
    });
}

【讨论】:

  • 是的,在构造函数中我已经有了“this.state = { calendars: [], gapi: null }”
  • 这意味着,您获取的数据不是数组,请在收到响应并检查后执行console.log(snapshot),同时将结果显示给我。
  • 返回一种树:V {A: O, V: U, g: rc}。我做了截图:link
  • 它不是一个数组,它是一个对象,你想在 V 或 V.A 或 V.V 或 V.g 上的哪个字段上使用地图?
猜你喜欢
  • 2020-01-29
  • 2021-12-09
  • 2017-02-21
  • 2022-01-12
  • 2021-12-04
  • 2021-12-17
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多