async.waterfall을 사용하여 데이터베이스에 대해 순차적으로 쿼리를 실행하므로 오류가있는 경우 모두 롤백 할 수 있습니다. 다음은 내 코드입니다 :Nodejs async.waterfall 콜백 인수 불일치?
function _getDBConnection(callback) {
\t try {
\t \t dbService.getConnection(function(connection) {
\t \t \t callback(null, connection); \t
\t \t });
\t } catch (e) {
\t \t callback(e, null);
\t }
}
function _insertNewUser(newUser, connection, callback) {
\t if (connection) {
\t \t var query = 'somequery';
\t \t // Start a transaction
\t \t connection.beginTransaction(function(error) {
\t \t \t if (error) {
\t \t \t \t callback(error, connection, null);
\t \t \t }
\t \t \t // Fire the query
\t \t \t connection.query(query, function(error, result) {
\t \t \t \t if (error) {
\t \t \t \t \t callback(error, connection, null)
\t \t \t \t } else {
\t \t \t \t \t callback(connection, newUser, result.insertId);
\t \t \t \t }
\t \t \t });
\t \t });
\t } else {
\t \t callback('Error: no connection in ', null, null);
\t }
};
module.exports.doRegister = function(req, res) {
\t // Collect all the params coming from the UI.
\t var newUser = {'object with UI params'};
\t console.log("Trying to register the user...");
\t async.series([
\t \t _getDBConnection,
\t \t async.apply(_insertNewUser, newUser)
\t ], function(err, success) {
\t \t if (err) {
\t \t \t console.log(err);
\t \t } else {
\t \t \t res.status(200).json({
\t \t \t \t success: true,
\t \t \t \t message: 'User registeration successfully.'
\t \t \t });
\t \t }
\t });
};
문제 : 제대로 두 번째 기능 (_insertNewUser)에 _getDBConnection 기능에서 연결 개체가 수신되지 않는 경우. 그것은 정의되지 않은 것으로오고 있으며 결과적으로 다음과 같은 오류가 발생합니다.
형식 오류 : 콜백 이미 호출 된 ~~~~ 스택 추적 ~~~~
나는 내가 생각 : connection.beginTransaction는 ~~~~ 스택 추적 ~~~~ 오류가 함수가 아닙니다 예상치 않은 _insertNewUser 함수에서 연결 개체 대신 '콜백'함수를 가져 오는 중입니다. 내가 도대체 뭘 잘못하고있는 겁니까?
중요한 경우 _insertNewUser 이후에 호출되는 다른 함수/쿼리가 있지만 코드 간결하게하기 위해 해당 함수를 제거했습니다.
'dbService.getConnection (기능 (연결)'? 난 당신이 dbService.getConnection'에 대한 래퍼를 (필요가 없다고 생각 ERR, 연결)'. –
심지어 한 후. 변화를하지 않습니다 그 변경, 나는 개체를 얻는 대신 _insertNewUser에서 연결을위한 [함수]를 얻었고 콜백은 정의되지 않았습니다. – legendofawesomeness