【问题标题】:TypeError: JSON.stringify(...).then is not a function - React JSTypeError: JSON.stringify(...).then 不是一个函数 - React JS
【发布时间】:2020-04-17 05:49:41
【问题描述】:

我正在尝试将我的 react 应用程序与登录页面的后端连接起来。我正在使用承诺返回成功消息。

登录.js

    onSubmitSignIn = () => {
        fetch('http://localhost:5000/', {
              method : 'post',
              headers :{ 'Content-Type' : 'application/json'},
              body : JSON.stringify({
                   userId : this.state.userId,
                   password : this.state.password
             }).then(response => response.json())
               .then(data => {
                    if(data === 'success'){
                            this.props.onRouteChange('home');
                    }
             })
        })
    }

后端代码 -

  exports.findById = (req) => {
         return new Promise((resolve) => {
               var sql = "Select * from users where userid = '" + req.body.userId + "' ;";
               connection.query(sql,req,  function (error, results, fields) {
                    var data = JSON.parse(JSON.stringify(results)); 
                    var valid = false; 
                    if( data.length !=0 && req.body.userId === data[0].userid && req.body.password === data[0].password)
                         valid = true; 

                    if(valid) {
                         resolve({message : "success"});
                    }else{
                         reject({ message :"fail"});
                    }
              });
        })
  };

点击登录按钮后,我收到一个错误“TypeError: JSON.stringify(...).then is not a function”

我尝试了一些类似问题的解决方案,但在我的情况下不起作用。

【问题讨论】:

  • 你确定变量results中有JSON格式的数据吗?还有额外的事情,我认为你通过 javascript 传递 sql 查询是不明智的

标签: json reactjs express


【解决方案1】:

你错过了一个括号。 JSON.stringify() 之后应该有一个右括号。

onSubmitSignIn = () => {
  fetch('http://localhost:5000/', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      userId: this.state.userId,
      password: this.state.password
    })
  }).then(response => response.json())
    .then((data) => {
      if (data === 'success') {
        this.props.onRouteChange('home');
      }
    });
};

【讨论】:

    【解决方案2】:

    then 应该在fetch 之外

    fetch('http://localhost:5000/', {
        method : 'post',
        headers :{ 'Content-Type' : 'application/json'},
        body : JSON.stringify({
             userId : this.state.userId,
             password : this.state.password
       })
    }).then(response => response.json())
      .then(data => {
        if(data === 'success'){
                this.props.onRouteChange('home');
        }
    })
    

    【讨论】:

      【解决方案3】:

      你有一个错字,.then 应该是 fetch 而不是 JSON.stringify

      onSubmitSignIn = () => {
        fetch("http://localhost:5000/", {
          method: "post",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            userId: this.state.userId,
            password: this.state.password
          })
        })
      //-^
          .then(response => response.json())
          .then(data => {
            if (data === "success") {
              this.props.onRouteChange("home");
            }
          });
      };
      
      

      【讨论】:

        猜你喜欢
        • 2017-06-24
        • 2016-02-19
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 2020-12-23
        • 2020-10-20
        • 2023-03-28
        相关资源
        最近更新 更多