简体   繁体   English

反应:无法读取未定义的属性“setState”

[英]React: Cannot read property 'setState' of undefined

I've got some data from Firebase Realtime Database and now I'm trying to add it to a variable.我从 Firebase 实时数据库中获得了一些数据,现在我正在尝试将其添加到一个变量中。 I'm sure that the querying works, because if I console.log(dataSnapshot) it logs the correct data (which is 001).我确定查询有效,因为如果我使用 console.log(dataSnapshot) 它会记录正确的数据(即 001)。 However, when I'm trying to create a variable out of that number I get the following error: Cannot read property 'setState' of undefined and it doesn't return anything to the console.但是,当我尝试使用该数字创建变量时,出现以下错误:无法读取未定义的属性“setState”,并且它不会向控制台返回任何内容。

Here's my code:这是我的代码:

class Table extends Component {
  constructor(props) {
    super(props);
    this.state = { timestamps: [], user: auth().currentUser, serial: "" };
  }

  componentDidMount() {
    const uid = this.state.user.uid;
    console.log(uid);
    let serial = this.state.serial;
    const serialRef = db.ref(uid + "/serial");
    serialRef.once("value").then(function (dataSnapshot) {
      console.log(dataSnapshot.val());
      this.setState({ serial: dataSnapshot.val() }, () => {
        serial = this.state.serial;
        console.log(serial);
      });
    });

Here's the screenshot of my console这是我的控制台的屏幕截图

Thanks in advance!提前致谢!

you are not using arrow callback function, If you don't use arrow callback you have to bind you function. If you don't want to write bind statement use arrow function which will get the context of the class automatically.您没有使用箭头回调 function,如果您不使用箭头回调,则必须绑定您 function。如果您不想编写绑定语句,请使用箭头 function,它将自动获取 class 的上下文。

 serialRef.once("value").then((dataSnapshot) => { console.log(dataSnapshot.val()); this.setState({ serial: dataSnapshot.val() }, () => { serial = this.state.serial; console.log(serial); }); });

Your problem is that the callback you pass to serialRef.once("value").then(...) does not have a "this" context set.您的问题是您传递给serialRef.once("value").then(...)的回调没有设置“this”上下文 If you change your code to this, it should work:如果您将代码更改为此,它应该可以工作:

componentDidMount() {
    const uid = this.state.user.uid;
    console.log(uid);
    let serial = this.state.serial;
    const serialRef = db.ref(uid + "/serial");
    // grab a reference to the current "this" context
    const table = this;
    serialRef.once("value").then(function (dataSnapshot) {
      console.log(dataSnapshot.val());

      // invoke setState on the captured context
      table.setState({ serial: dataSnapshot.val() }, () => {
        serial = table.state.serial;
        console.log(serial);
      });
    });

Or you could use a lambda function, like you already do in the setState call :或者您可以使用 lambda function,就像您在 setState 调用中所做的那样:

componentDidMount() {
    const uid = this.state.user.uid;
    console.log(uid);
    let serial = this.state.serial;
    const serialRef = db.ref(uid + "/serial");
     // "Fat arrow" or "lambda" functions implicitly capture the current "this" context
    serialRef.once("value").then((dataSnapshot) => {
      console.log(dataSnapshot.val());
      this.setState({ serial: dataSnapshot.val() }, () => {
        serial = this.state.serial;
        console.log(serial);
      });
    });

the arrow function should retain your this context箭头 function 应保留您的 this 上下文

serialRef.once("value").then((dataSnapshot) => {
     console.log(dataSnapshot.val());
     this.setState({ serial: dataSnapshot.val() }, () => {
       serial = this.state.serial;
       console.log(serial);
     });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM