最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

为什么我无法从api获得正确的响应?

IT培训 admin 7浏览 0评论

为什么我无法从api获得正确的响应?

例如,我尝试从api读取“ true”或“ false”来为用户提供授权,但是我得到的是未定义的数据。这两种方法都可以从发送数据到验证用户的完美工作。

我在服务器内部有此api方法:


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

以及我的react app中的此调用api:

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

    }

谢谢。

回答如下:

考虑您正在使用的功能的异步性质。它可能看起来像这样:

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

fetch

为什么我无法从api获得正确的响应?

例如,我尝试从api读取“ true”或“ false”来为用户提供授权,但是我得到的是未定义的数据。这两种方法都可以从发送数据到验证用户的完美工作。

我在服务器内部有此api方法:


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

以及我的react app中的此调用api:

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

    }

谢谢。

回答如下:

考虑您正在使用的功能的异步性质。它可能看起来像这样:

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

fetch

发布评论

评论列表 (0)

  1. 暂无评论