0

I can't get a good answer from my api , for example I try to read "true" or "false" from api to give the authorization for user , but I got just undefined data. the methods both work prefectly from sending data to verify the user .

I have this api method inside server :


router.get("/sickers/user/login/:mail/:pass", (req, res) => {
    //var values = JSON.parse(req.body);
    var pass = req.params.pass;
    var email = req.params.mail;
    //console.log(values);
    if (pass !== null || pass !== "") {
        try {
            con.connect();
            con.query("SELECT Password FROM `sickers` WHERE Email='" + email + "'", function(err, rows, field) {
                if (err) {
                    console.log(err);
                    res.send("an error detected try later");
                } else {
                    try {

                        if (pass == rows[0].Password) {

                            res.json({ "result": "true" })
                        } else {
                            res.json({ "result": "false" })
                        }
                    } catch {

                        res.json({ "result": "false" })
                    }

                }
            });
        } catch (e) {
            res.send("no data found");
            console.log("obj not found");
        }
    }
 con.end();
});

and this call api inside my react app :

submithandler(e) {
   e.preventDefault();
        const url = 'http://localhost:8000/api/sickers/user/login/'+this.state.email+'/'+this.state.password+'';
        const res =  fetch(url);
        const data = res;
        this.setState({result:data});
       alert(this.state.result);

    }

thanks.

azdeviz
  • 597
  • 2
  • 9
  • 20
  • Both `fetch` and `setState` are async. You'll need to await the `fetch` call, or add the rest of the code in a `.then` callback, and alert from the `setState` callback in order for things to execute in the order you expect. – Brian Thompson May 14 '20 at 18:49
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Brian Thompson May 14 '20 at 18:59

1 Answers1

1

Account for the async nature of the functions you are using. It might look something like this:

const url = 'http://localhost:8000/api/sickers/user/login/'+this.state.email+'/'+this.state.password+'';
// Use .then to call a function AFTER the fetch has completed
fetch(url).then(result => result.json()).then((response) => {
  // Use the setState callback to check updated values AFTER they have been updated
  this.setState({result: response}, () => {
    alert(this.state.result);
  });
});

Docs on fetch

Docs on setState

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43
  • Thanks for reply , I have tried this , I still get "undefined" anser , I tried this.state.result.result cause I have json froma sent from api like : { "result": "true" } , so I need to get the value of result inside json every thing shows me "undefined" . – azdeviz May 14 '20 at 19:37