未捕获(承诺)TypeError:无法读取undefined的属性'bind'

时间:2018-05-23 01:36:03

标签: javascript reactjs variables constructor this

这是我的代码:

const cookies = new Cookies();
var name = (cookies.get('name'));
var key = (cookies.get('key'));
console.log(key);
const theme = createMuiTheme({
  palette: {
    primary: amber,
    secondary: blue
  },
  root: {
    flexGrow: 1,
  },
  bottomView: {

    width: '25%',
    height: 50,
    backgroundColor: '#FF9800',
    justifyContent: 'center',
    alignItems: 'center',
    position: 'absolute',
    bottom: 0
  },
});
class Base {
  constructor() {}
}

class Homework extends Base {
  constructor() {
    super();

    this.state = {
      topicBox: null,
      payloadBox: null
    };

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

  async loadTest() {
    try {
      //grab courses
      const response = await fetch('https://classroom.googleapis.com/v1/courses?access_token=' + key);
      const json = await response.json();
      //coursemax vars will eventually be user defined
      var coursemax = [2, 2, 2, 2, 2, 2, 2, 2];

      if (json.courses != null) {
        for (var course = 0; course < json.courses.length && course < 9; course++) {
          //grab course info
          var coursework = await fetch('https://classroom.googleapis.com/v1/courses/' + json.courses[course].id + '/courseWork?access_token=' + key);
          var coursejson = await coursework.json();
          console.log(coursejson);
          var assignment = "";
          for (var assignmentnum in coursejson.courseWork) {
            if (assignmentnum <= coursemax[course] - 1) {
              //add in assignment
              assignment += "<p>" + coursejson.courseWork[assignmentnum].title + "</p>";
              //"Due: "+coursejson.courseWork[assignmentnum].dueDate.month+"/"+coursejson.courseWork[assignmentnum].dueDate.day
            }
          }
          //making ids to render
          document.getElementById('class' + (course + 1) + 'info').innerHTML = assignment;
          document.getElementById('class' + (course + 1)).innerHTML = json.courses[course].name + '</b>' + ':' + '<br/>';;
        }
      }
    } catch (err) {
      console.log(err);
    }
  }
}
var app = new Homework();
var config = {
  apiKey: "xxx",
  authDomain: "xxx",
  databaseURL: "xxx",
  projectId: "xxx",
  storageBucket: "xxx",
  messagingSenderId: "xxx"
};
firebase.initializeApp(config);
app.loadTest();

function publish() {
  console.log(this.state.topicBox, this.state.payloadBox);
  const itemsRef = firebase.database().ref('Users');
  const item = {
    title: this.state.topicBox,
    user: this.state.payloadBox
  }
  itemsRef.push(item).set("name");
  this.setState({
    topicBox: '',
    payloadBox: ''
  });

}

const LoginPage = () =>
<MuiThemeProvider theme={theme}>
      <div>
        <AppBar position="static" id='title'>
          <Toolbar>
            <Typography type="title" color='inherit'>
              MVHS Homework App
            </Typography>
            <div id='avatar' color='inherit'><Avatar>{name[0]}</Avatar></div>
          </Toolbar>
        </AppBar>
        <Paper id='calendar'>
          <Table>
            <TableHead>
              <TableRow>
                <TableCell><p id = "class1"></p></TableCell>
                <TableCell><p id = "class1info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class2"></p></TableCell>
                <TableCell><p id = "class2info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class3"></p></TableCell>
                <TableCell><p id = "class3info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class4"></p></TableCell>
                <TableCell><p id = "class4info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class5"></p></TableCell>
                <TableCell><p id = "class5info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class6"></p></TableCell>
                <TableCell><p id = "class6info"></p></TableCell>
              </TableRow>
              <TableRow>
                <TableCell><p id = "class7"></p></TableCell>
                <TableCell><p id = "class7info"></p></TableCell>
              </TableRow>
            </TableHead>
          </Table>
        </Paper>
      <input 
        type="text" 
        name="topicBox" 
        placeholder="Name" 
        value={ this.state.topicBox }
        onChange={ this.handleChange } 
      />
      <input 
        type="text" 
        name="payloadBox" 
        placeholder="Details"
        value={ this.state.payloadBox } 
        onChange={ this.handleChange } 
      />
      <Button variant="raised" color="secondary" style = {theme.bottomView} onClick={ this.publish }>
        Secondary
      </Button>
      </div>

    </MuiThemeProvider>



export default LoginPage

在上面的代码中,出现错误:

  

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

错误是:

  

未捕获(承诺)TypeError:无法读取属性'bind'   未定义

为什么会这样?

编辑: 我认为错误是目前代码的唯一问题。如果有错误,请随时告诉我。 :)

1 个答案:

答案 0 :(得分:0)

你错过了handleChange方法。

handleChange(event) {
    this.setState({value: event.target.value});
  }

...

<input type="text" value={this.state.value} onChange={this.handleChange} />

PS: 请更具体,错误引用什么?

 Cannot read property 'bind' of undefined