未处理的拒绝(TypeError):无法在“ Window”上执行“ fetch”:无效的名称

时间:2019-07-18 18:53:37

标签: javascript reactjs npm

尝试注册用户信息时出现以下错误:

  

未处理的拒绝(TypeError):无法在以下位置执行“提取”   “窗口”:名称无效:Register.onSubmitRegister

寄存器组件 ( Register.js

import React, {Component} from 'react';     

class Register extends Component{
    constructor(props){
    super(props);
    this.state = {
      name:'',
      password:'',
      email:''
    }
  }
  onSubmitRegister = () => {  
// this is where the error occurs don't know why?
    fetch('http://localhost:3000/register', {
      method:'post',
      headers: {'Content/Type' : 'application/json'},
        body: JSON.stringify({
          name:this.state.name,
          password:this.state.password,
          email:this.state.email
        })
    })
      .then( response => response.json())
      .then(user =>{
        if(user){
          this.props.loaduser(user);
          this.props.onRouteChange('home');
        }
      })
  }

服务器端 server.js

app.post('/register', (req, res)=> {
    const {email, password, name} = req.body;
    dataBase.users.push({
        id:"132",
        name:name,
        email:email,
        entries:0,
        joined: new Date()
    })
   res.json(dataBase.users[dataBase.users.length - 1]);     
})
app.listen(3000)

0 个答案:

没有答案