2016-08-18 11 views
0

私はNode.jsが非同期であることは知っていますが、なぜ2つのクエリが実行されていないのかわかりません。最初のクエリだけです。最初のクエリがOkの場合はクエリを挿入しようとしましたが、2番目のクエリは実行されません。Node.js - 2つのクエリが実行されないのはなぜですか?

sql = "INSERT INTO TB_DISPOSITIVOS_VOZ (DS_CLIENTID, FK_ID_GRUPO) "; 
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + ") "; 

connection.query(sql, function(erro2, rows2, fields2) { 

    if (!erro2) { 
    console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId);          
    } else { 
    console.log("Error - When inserting alexa in the group" + erro2); 
    context.fail("Error - When inserting alexa in the group" + erro2); 
    } 

}); 

sql1 = "DELETE FROM TB_FILA_PAREAMENTO_OTP WHERE DS_CLIENTID = '" + alexaClientId + "'"; 

connection.query(sql1, function(erro3, rows3, fields3) { 

    if (!erro3) { 
    console.log("Query Delete TB_FILA_PAREAMENTO_OTP OK"); 
    } else { 
    console.log("Error - Delete query does not works correctly..." + erro3); 
    context.fail("Error - Delete query does not works correctly..." + erro3); 
    } 

}); 
+0

このような複数のステートメントを使用して

? – mrdotb

答えて

0

使用することができます。

は、ここに私のコードです。接続中

フラグmultistatements:あなたはMySQLの使用に何のパッケージ

multipleStatements: true 

sql = "INSERT INTO TB_1 (DS_CLIENTID, FK_ID_GRUPO) "; 
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + "); "; 
sql1 = "DELETE FROM TB_2 WHERE DS_CLIENTID = '" + alexaClientId + "';"; 
sql2 = "DELETE FROM TB_3 WHERE DS_CLIENTID = '" + alexaClientId + "';"; 

sql = sql2 + sql + sql1; 

connection.query(sql,[1,2,3], function(erro2, rows2, fields2) { 
    if (!erro2) { 
     console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId); 
    } else { 
     console.log("Error - When inserting alexa in the group" + erro2); 
     context.fail("Error - When inserting alexa in the group" + erro2); 
    } 
}); 
0

あなたは、私は私の問題を解決しQ github promise

var Q = require('Q'); 



function query1(){ 

var defered = Q.defer(); 
sql = "INSERT INTO TB_DISPOSITIVOS_VOZ (DS_CLIENTID, FK_ID_GRUPO) "; 
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + ") "; 

connection.query(sql, function(erro2, rows2, fields2) { 

    if (!erro2) { 
     console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId); 

    } else { 
     console.log("Error - When inserting alexa in the group" + erro2); 
     context.fail("Error - When inserting alexa in the group" + erro2); 
    } 

}); 

return defered.promise; 

} 



function query2(){ 
var defered = Q.defer(); 
sql1 = "DELETE FROM TB_FILA_PAREAMENTO_OTP WHERE DS_CLIENTID = '" + alexaClientId + "'"; 

connection.query(sql1, function(erro3, rows3, fields3) { 

    if (!erro3) { 
     console.log("Query Delete TB_FILA_PAREAMENTO_OTP OK"); 
    } else { 
     console.log("Error - Delete query does not works correctly..." + erro3); 
     context.fail("Error - Delete query does not works correctly..." + erro3); 
    } 

});  
return defered.promise; 
} 

Q.all([query1(),query2()]).then(function(results){ 

     console.log('all done'); 
    }); 
関連する問題