3

I am trying to connect my react app with the backend for log in page. I am using a promise to return a success message.

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

Backend code -

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

After clicking on sign in button, I am getting an error "TypeError: JSON.stringify(...).then is not a function"

I tried some solutions from similar questions, it did not work in my case.

1
  • you sure there is a JSON format data in variable results? and extra thing, I don't think is wise for u to passing sql query via javascript Commented Dec 27, 2019 at 7:59

4 Answers 4

5

The then should be outside of 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');
    }
})
Sign up to request clarification or add additional context in comments.

Comments

2

You have a typo, .then should be on fetch not on 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");
      }
    });
};

Comments

0

you have missed a bracket. there should be a closing bracket after 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');
      }
    });
};

Comments

0

I had this problem too. Check and confirm that you're not importing or requiring {JSON} in your application. It's most likely referring to that imported JSON rather than the global

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.