Node.js-数据正在插入,但我没有得到响应

时间:2019-02-03 07:54:09

标签: mysql node.js express

我正在将mysql与Nodejs一起使用,我的要求是检查用户是否存在,如果用户不存在,我们应该创建该用户。

router.post('/signup', (req, res) => {
    var name = req.body.name;
    var phone = req.body.phone;
    var password = req.body.password;
    var datecreated = req.body.datecreated;
    var dateupdated = req.body.dateupdated;

    db.query("select * from users where name='" + name + "' and password='" + password + "'", (error, rows) => {
        if (error) {
            res.send({ "status": "failure", "Error": error });
        }
        else if (rows.length > 0) {
            res.send({ "status": "exists" });
        }
        else {
            db.query("insert into users(name, password, phone, datecreated, dateupdated) values('" + name + "', '" + password + "', '" + phone + "', '" + datecreated + "', '" + dateupdated + "')"), (error, rows) => {
                if (error) {
                    res.send({ "status": "failure", "Error": error });
                }
                else {
                    res.send({ "status": "success" });
                }
            }
        }
    });
});

如果存在用户,我将得到响应作为状态,但是如果我发送新用户,则数据将被插入,但我没有得到响应。需要一些帮助的人。

工作解决方案:

db.query("select * from users where name='" + name + "' and password='" + password + "'", (error, rows) => {
        if (error) {
            res.send({ "status": "failure", "Error": error });
        }
        else if (rows.length > 0) {
            res.send({ "status": "exists" });
        }
        else {
            db.query("insert into users(name, password, phone, datecreated, dateupdated) values('" + name + "', '" + password + "', '" + phone + "', '" + datecreated + "', '" + dateupdated + "')", (error, rows) => {
                if (error) {
                    res.send({ "status": "failure", "Error": error });
                    res.end();
                }
                else {
                    res.send({ "status": "success" });
                    res.end();
                }
            })
        }
    });

1 个答案:

答案 0 :(得分:0)

问题出在此代码中。 如果未发现错误,则使用send()方法发送行数。成功执行另一个查询后,将调用另一个send()方法,但该方法无效,因为send()在发送数据后终止了连接。您只能运行一次send(),因为关闭连接后其他send()将无法运行。而是使用res.write(),它本身不会关闭连接。 send()在内部实现res.write(),紧随其后的是res.end()

if (error) {
            res.send({ "status": "failure", "Error": error });
        }
        else if (rows.length > 0) {
            res.send({ "status": "exists" });
        }
        else {
            db.query("insert into users(name, password, phone, datecreated, dateupdated) values('" + name + "', '" + password + "', '" + phone + "', '" + datecreated + "', '" + dateupdated + "')"), (error, rows) => {
                if (error) {
                    res.send({ "status": "failure", "Error": error });
                }
                else {
                    res.send({ "status": "success" });
                }
            }

代码应为

  if (error) {
            res.write({ "status": "failure", "Error": error });
        }
        else if (rows.length > 0) {
            res.write({ "status": "exists" });
        }
        else {
            db.query("insert into users(name, password, phone, datecreated, dateupdated) values('" + name + "', '" + password + "', '" + phone + "', '" + datecreated + "', '" + dateupdated + "')"), (error, rows) => {
                if (error) {
                    res.write({ "status": "failure", "Error": error });
                    res.end()
                }
                else {
                    res.write({ "status": "success" });
                    res.end()
                }
            }