2016-08-18 81 views
0

我知道Node.js是異步的,但我不知道爲什麼我的兩個查詢沒有執行,只有第一個查詢。如果第一個查詢是OK,我只是試圖放置查詢,但即使第二個查詢不執行。Node.js - 爲什麼我的兩個查詢沒有執行?

這裏是我的代碼:

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

你用什麼包的mysql? – mrdotb

回答

0

我解決我的問題。

使用多條語句是這樣的:

標誌multistatements在連接:

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'); 
    }); 
相關問題