我在我的项目中使用异步模块版本0.9.0和sequelize模块1.7.9。使用Async模块的自动功能逐个调用sequelize功能。我需要在第一个函数完成时调用第二个函数并返回它想要返回的值。
exports.createUser = function (req, authenticatedUser, res) {
configurationHolder.sequelize.transaction(function (t) {
async.auto({
checkAuthority:checkAuthorityToCreateUserWrapper(authenticatedUser, req.body.authority, res,t),
checkEmail: ['checkAuthority',checkEmailWrapper(req.body.email, req.body.username, res,t)],
address: ['checkEmail','checkAuthority',createAddressWrapper(t, req.body.lineOne, req.body.lineTwo, req.body.pin, req.body.cityId, req.body.countryId, req.body.regionId, res)],
role: ['checkEmail','checkAuthority','address', findRoleWrapper(t, req, req.body.authority, authenticatedUser, res)],
user: ['checkEmail','checkAuthority','address','role',createUserWrapper(t, req.body.password, req.body.firstName, req.body.lastName, req.body.gender, req.body.email, res)],
userWithRole: ['checkEmail','checkAuthority','address','role', addRoleToUserWrapper(t, req.body.authority, res)],
userWithAddress: ['checkEmail','checkAuthority','address','role', 'userWithRole', addAddressToUserWrapper(t, req, res)],
authorizationToken: ['checkEmail','checkAuthority','address','role', 'userWithRole','userWithAddress', createAuthorizationTokenWrapper(t, req, res)],
userWithAuthorizationToken: ['checkEmail','checkAuthority','address','role', 'userWithRole','userWithAddress', 'authorizationToken', addAuthorizationTokenToUserWrapper(t, req, res)]
}, function (err, results) {
console.log("err === " + err)
if (err) {
console.log("err ================================ " + err)
var error = err
t.rollback().success(function (err) {
configurationHolder.responseUtil.responseHandler(res, results, error, true, 200)
})
} else {
t.commit().success(function (err) {
configurationHolder.responseUtil.responseHandler(res, results, null, true, 200)
})
}
})
});
}
代码按照定义的顺序执行Async中提到的每个函数,但在结果中它只返回前4个函数的值,即checkAuthority,checkEmail,address,role。
我使用打印时间戳进行调试,在完成第一个函数之前我觉得在调用下一个函数。
完整代码如下:
exports.createUser = function (req, authenticatedUser, res) {
configurationHolder.sequelize.transaction(function (t) {
async.auto({
checkAuthority:checkAuthorityToCreateUserWrapper(authenticatedUser, req.body.authority, res,t),
checkEmail: ['checkAuthority',checkEmailWrapper(req.body.email, req.body.username, res,t)],
address: ['checkEmail','checkAuthority',createAddressWrapper(t, req.body.lineOne, req.body.lineTwo, req.body.pin, req.body.cityId, req.body.countryId, req.body.regionId, res)],
role: ['checkEmail','checkAuthority','address', findRoleWrapper(t, req, req.body.authority, authenticatedUser, res)],
user: ['checkEmail','checkAuthority','address','role',createUserWrapper(t, req.body.password, req.body.firstName, req.body.lastName, req.body.gender, req.body.email, res)],
userWithRole: ['checkEmail','checkAuthority','address','role', addRoleToUserWrapper(t, req.body.authority, res)],
userWithAddress: ['checkEmail','checkAuthority','address','role', 'userWithRole', addAddressToUserWrapper(t, req, res)],
authorizationToken: ['checkEmail','checkAuthority','address','role', 'userWithRole','userWithAddress', createAuthorizationTokenWrapper(t, req, res)],
userWithAuthorizationToken: ['checkEmail','checkAuthority','address','role', 'userWithRole','userWithAddress', 'authorizationToken', addAuthorizationTokenToUserWrapper(t, req, res)]
}, function (err, results) {
console.log("err === " + err)
if (err) {
console.log("err ================================ " + err)
var error = err
t.rollback().success(function (err) {
configurationHolder.responseUtil.responseHandler(res, results, error, true, 200)
})
} else {
t.commit().success(function (err) {
configurationHolder.responseUtil.responseHandler(res, results, null, true, 200)
})
}
})
});
}
//wrapper function starts here
function checkAuthorityToCreateUserWrapper(authenticatedUser, authority, res,t){
return function(next){
checkAuthorityToCreateUser(authenticatedUser, authority, res,next,t)
}
}
function createAddressWrapper(t, lineOne, lineTwo, pin, cityId, countryId, regionId, res,t) {
console.log("ADDRESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS")
return function (next, results) {
createAddress(t, lineOne, lineTwo, pin, cityId, countryId, regionId, next, results, res,t)
}
}
function addRoleToUserWrapper(t, authority, res) {
return function (next, results) {
addRoleToUser(t,next, results,authority, res)
}
}
function addAddressToUserWrapper(t, req, res) {
return function (next, results) {
addAddressToUser(t, req, res, next, results)
}
}
function findRoleWrapper(t, req, authority, authenticatedUser, res) {
return function (next, results) {
findRole(t, next, results, authority, authenticatedUser, res)
}
}
function createUserWrapper(t, password, firstName, lastName, gender, email, res) {
return function (next, results) {
createUserExecution(t,next, results, password, firstName, lastName, gender, email)
}
}
function checkEmailWrapper(email, username, res,t) {
return function (next,results) {
checkEmail(next,results, email, username, res,t)
}
}
function createAuthorizationTokenWrapper(t, req, res) {
return function (next, results) {
createAuthorizationToken(next, results, t, req, res)
}
}
function addAuthorizationTokenToUserWrapper(t, req, res) {
return function (next, results) {
addAuthorizationTokenToUser(next, results, t, req, res)
}
}
//wrapper function ends here
function checkEmail(next,results, email, username, res,t) {
domain.AuthorizationToken.find({
where: configurationHolder.sequelize.or({
"email": email
}, {
"username": username
})
},{transaction:t}).complete(function (err, emailCheckUser) {
if (err != null) {
next(err,null)
// configurationHolder.responseUtil.responseHandler(res, null, configurationHolder.errorMessage.unique, true, 400)
} else if (emailCheckUser != null) {
next(configurationHolder.errorMessage.unique, null)
//configurationHolder.responseUtil.responseHandler(res, null, configurationHolder.errorMessage.unique, true, 400)
} else{
domain.User.find({where:{"email":email}}).complete(function(err,userCheck){
if(err != null || userCheck != null){
next(configurationHolder.errorMessage.unique, null)
}else{
next(err,{"checkemail":"available"})
}
})
}
})
}
function checkAuthorityToCreateUser(authenticatedUser, authority, res,next,t) {
var err = configurationHolder.errorMessage.failedLogin
console.log("authenticatedUser.role.authority = " + authenticatedUser.role.authority + " " + authority)
switch (authenticatedUser.role.authority) {
case "SUPERADMIN":
if (authority == "SUPERADMIN_CUSTOMER_SERVICES" || authority == "ADMIN" || authority == "DELEGATE_ADMIN" || authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
// configurationHolder.responseUtil.responseHandler(res, null, , true, 400)
}
case "SUPERADMIN_CUSTOMER_SERVICES":
if (authority == "ADMIN" || authority == "DELEGATE_ADMIN" || authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
}
case "ADMIN":
if (authority == "DELEGATE_ADMIN" || authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
}
case "DELEGATE_ADMIN":
if (authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
}
case "USER":
if (authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
}
default:
if (authority == "USER") {
next(null,{allowed:true})
} else {
next(configurationHolder.errorMessage.notPermit,{allowed:true})
}
}
}
function createAddress(t, lineOne, lineTwo, pin, cityId, countryId, regionId, next, results, res) {
domain.Address.create({
line_one: lineOne,
line_two: lineTwo,
pincode: pin,
cityId: cityId,
countryId: countryId,
regionId: regionId
}, {
transaction: t
}).complete(function (err, address) {
if (err != null) {
next(err,null)
// t.rollback()
// configurationHolder.responseUtil.responseHandler(res, null, err, true, 400)
} else {
console.log("address = " + address)
next(null, address)
}
})
}
function findRole(t, next, results, authority, authenticatedUser, res) {
domain.Role.find({
where: {
authority: authority
}
}, {
transaction: t
}).complete(function (err, role) {
console.log("role == "+err)
next(err,role)
})
}
function createUserExecution(t,next, results, password, firstName, lastName, gender, email) {
password = crypto.createHmac('sha1', "Admin").update(password).digest('hex')
domain.User.create({
firstName: firstName,
lastName: lastName,
gender: gender,
email: email,
salt: email,
delflag: false
}, {
transaction: t
}).complete(function (err, user) {
console.log(err);
next(err, user)
})
}
function addRoleToUser(t,next, results,authority, res) {
results.role.setUsers([results.user], {
transaction: t
}).complete(next)
}
function addAddressToUser(t, req, res, next, results) {
results.address.setUsers([results.user], {
transaction: t
}).complete(function (err, obj) {
console.log(err)
next(err, obj)
})
}
function createAuthorizationToken(next, results, t, req, res) {
password = crypto.createHmac('sha1', "Admin").update(req.body.password.toString()).digest('hex')
domain.AuthorizationToken.create({
authToken: null,
email: req.body.email,
password: password,
username: req.body.username
}, {
transaction: t
}).complete(function (err, obj) {
console.log(err)
next(err, obj)
})
}
function addAuthorizationTokenToUser(next, results, t, req, res) {
results.user.setAuthorizationToken(results.authorizationToken, {
transaction: t
}).complete(function (err, obj) {
console.log(err)
next(err, obj)
})
}
我得到的结果是:
{
"error": true,
"message": null,
"response": {
"checkAuthority": {
"allowed": true
},
"checkEmail": {
"checkemail": "available"
},
"address": {
"line_one": "!soulispure#9911",
"line_two": "abhimanyu.mailme@gmail.com",
"pincode": 110062,
"cityId": 98,
"countryId": 97,
"regionId": 99,
"id": 84
},
"role": {
"id": 5,
"authority": "USER",
"createdAt": "2014-11-26T12:24:08.000Z",
"updatedAt": "2014-11-26T12:24:08.000Z",
"deletedAt": null
}
}
}