我需要运行两个查询

时间:2018-02-14 16:38:11

标签: javascript mysql sql node.js

我有一个应用程序,我在一个带有标识符的数据库中输入新记录。

我的第一个语句是包含整个文档的if语句:

if(body['id'] && body['business'] && body['contact_no'] && body['contact_email'] && body['addr_1'] && body['addr_2'] && body['post_code'] && body['gs_unit'] && body['timezone']){

     (everything goes here)
}

上面的if语句只是确保所有内容都有值(或者它存在)。

下一步是运行一个查询,检查是否注册了该ID:

var sql1 = "SELECT * FROM epas_plc WHERE uid = ?";

conn.query(sql1,[id], function (err1, resp1) {

    console.log(resp1.length);
    var rlength = resp1.length;

    if(resp1.length == 1){
            res.send({'status' : 'error', 'information' : 'id already exsists'});
            return;
    }

});

上述查询有效,如果匹配则会列出响应。 如果没有匹配,则应执行下一个查询,rlength如上面的查询中所定义:

if (rlength == 0) {
    var sql = "INSERT INTO epas_plc (uid, business, contact_no, contact_email, addr_1, addr_2, post_code, gs_unit, timezone) VALUES(?,?,?,?,?,?,?,?,?)";

    conn.query(sql,[id, body['business'], body['contact_no'], body['contact_email'], body['addr_1'], body['addr_2'], body['post_code'], body['gs_unit'], body['timezone']], function(error, response){

        if(error) throw error;

        res.send({'status' : 'success', 'information' : 'successfully created entry with id: ' + req.body.id});
    })

}

不幸的是,这不起作为Can't set headers after they are sent.

有谁知道如何解决这个问题,和/或嵌套查询?

编辑:SQL模块是MYSQL

2 个答案:

答案 0 :(得分:1)

我可以在代码中看到一些问题,例如return语句之后的send以及何时检查错误。我提出这个解决方案:

var sql1 = "SELECT * FROM epas_plc WHERE uid = ?";

conn.query(sql1, [id], function(err1, resp1) {

    console.log(resp1.length);
    var rlength = resp1.length;
    if (err1) res.status(500).send({
        'msg': 'error found'
    });

    if (rlength == 1) {
        res.send({
            'status': 'error',
            'information': 'id already exsists'
        });
    } else {
        var sql = "INSERT INTO epas_plc (uid, business, contact_no, contact_email, addr_1, addr_2, post_code, gs_unit, timezone) VALUES(?,?,?,?,?,?,?,?,?)";

        conn.query(sql, [id, body['business'], body['contact_no'], body['contact_email'], body['addr_1'], body['addr_2'], body['post_code'], body['gs_unit'], body['timezone']], function(error, response) {

            if (error) res.status(500).send({
                'msg': 'error found'
            });

            res.send({
                'status': 'success',
                'information': 'successfully created entry with id: ' + req.body.id
            });
        })
    }

});

保持代码清晰很重要,使用var rlength = resp1.length;并使用if(resp1.length == 1){检查后没有意义。如果发现某些错误,您可以将其发送给谁调用,因为它在请求范围内。

答案 1 :(得分:0)

我认为你遇到的问题是'范围'和'异步'相关。

首先,范围:

这里的'范围'问题是在第一个查询的函数中声明了var rlength = resp1.length;

这意味着如果您有两个内联的查询,如:

conn.query(sql1,[id], function (err1, resp1) {
    // ... 
    var rlength = resp1.length;
    // ... 
});

// You will find that 'rlength' is not defined
// console.log(typeof rlength);
if (rlength == 0) {
    // ...
}

这可以通过将'if语句'放在第一个查询函数中来解决,如下所示:

conn.query(sql1,[id], function (err1, resp1) {
    // ...
    var rlength = resp1.length;
    // ...
    if (rlength == 0) {
        // ...
    }
});

其次,异步

如果代码的结构如下所示,这个问题就不那么明显了:

conn.query(sql1,[id], function (err1, resp1) {
    // ...
});

if (rlength == 0) {
    // ...
}

'if语句'将独立于查询是否完成而执行。这意味着'if语句'可以在查询之前执行