1
votes

TypeError: JSON.stringify (...). Then is not a function - React JS

J'essaie de connecter mon application react au backend pour la page de connexion. J'utilise une promesse pour renvoyer un message de réussite.

login.js

  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"});
                    }
              });
        })
  };

Code backend -

    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');
                    }
             })
        })
    }

Après avoir cliqué sur le bouton de connexion, j'obtiens un erreur "TypeError: JSON.stringify (...). then is not a function"

J'ai essayé des solutions à partir de questions similaires, cela n'a pas fonctionné dans mon cas.


1 commentaires

vous êtes sûr qu'il y a des données au format JSON dans la variable results ? et chose supplémentaire, je ne pense pas qu'il soit sage pour vous de passer une requête SQL via javascript


3 Réponses :


0
votes

vous avez manqué une parenthèse. il devrait y avoir un crochet fermant après 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');
      }
    });
};


0 commentaires

3
votes

Le alors doit être en dehors de 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');
    }
})


0 commentaires

1
votes

Vous avez une faute de frappe, . alors devrait être sur fetch et non sur 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");
      }
    });
};


0 commentaires