Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node JS mySQL三重查询和信息丢失_Mysql_Sql_Node.js_Database - Fatal编程技术网

Node JS mySQL三重查询和信息丢失

Node JS mySQL三重查询和信息丢失,mysql,sql,node.js,database,Mysql,Sql,Node.js,Database,在我的应用程序中,我有两个不同的表,它们通过第一个表的ID相互关联(一对多关系)。它应该首先以JSON格式从前端收集数据,如下所示: cancellation = { name: someting id: someting rule = [ {someting}, {something}, {something} ] } 一张桌子是取消的,第二张桌子是规则的。如果我想将这些信息按此顺序排列,我需要先插入一条记录以便取消。然后进行查询,找出数据库中该记录的ID,然后插入使用该ID作为外键的

在我的应用程序中,我有两个不同的表,它们通过第一个表的ID相互关联(一对多关系)。它应该首先以JSON格式从前端收集数据,如下所示:

cancellation = {
name: someting
id: someting
rule = 
[
 {someting}, {something}, {something}
] 
}
一张桌子是取消的,第二张桌子是规则的。如果我想将这些信息按此顺序排列,我需要先插入一条记录以便取消。然后进行查询,找出数据库中该记录的ID,然后插入使用该ID作为外键的所有规则。但是由于Node JS是异步的,所以在获取有关记录程序的ID的信息之前,要执行代码的其余部分,并将该变量视为未定义的。< /P>
app.post('/databaseSend/cancellation', function(req,res){
var cancellationReceived = req.body;
var cancellationID;
var rules = [];
var cancellation = [];
cancellation[0] = 
[
    cancellationReceived.name,
    cancellationReceived.id
]
// inserting data into cancellation table
connection.query("INSERT INTO cancellations (name, User_ID) VALUES ?", [cancellation],
    function(err,results){
        if(err){console.log(err)}

    }
)
//fetching ID of the current record
connection.query("SELECT id FROM cancellations WHERE User_ID = ? AND name = ?", [cancellationReceived.id, cancellationReceived.name],
    function(err, results){
        var cancellationID = results[0].id;
    });


//assigning ID to use it as a foreign key
for(var i = 0; i < cancellationReceived.rule.length; i++)
{
    rules[i] = 
    [
        cancellationReceived.rule[i].daysBefore,
        cancellationReceived.rule[i].fee,
        cancellationReceived.rule[i].type,
        cancellationID
    ]
}

for(var i = 0; i < rules.length; i++)
{
    console.log(rules[i]); // ID is undefined
}

});
app.post('/databaseSend/cancellation',函数(req,res){
var cancellationReceived=请求主体;
var取消ID;
var规则=[];
var取消=[];
取消[0]=
[
cancellationReceived.name,
取消已收到。id
]
//将数据插入取消表
connection.query(“插入取消(名称、用户ID)值?”,[cancellation],
功能(错误、结果){
if(err){console.log(err)}
}
)
//正在获取当前记录的ID
connection.query(“从取消中选择id,其中User_id=?和name=?”,[cancellationReceived.id,cancellationReceived.name],
功能(错误、结果){
var cancellationID=results[0].id;
});
//分配ID以将其用作外键
对于(变量i=0;i
我怎样才能解决这个问题?我尝试使用setTimeout暂停代码,但它没有改变任何东西


我在mysql中使用这个节点模块->

解决这个问题的最好方法是RTFM

connection.query('INSERT INTO cancellations (name, user_id) values ?', [cancellation], function(err, results) {
    if (err)
       return console.error(err);

    // See https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row
    var cancellation_id = results.insertId;

    // Generate sql for rules, join them by ; and execute as one query
    // See https://github.com/mysqljs/mysql#multiple-statement-queries 
    connection.query(sqls, function(err) {
        if (err)
             return console.error(err);

        // Send response here
    });
})