请教这段数据库代码中的回调该怎么拉平
发布于 11 年前 作者 gastrodia 5169 次预览 最后一次回复是 11 年前 来自
knex('user').where('username',postUser.username).select().then(function(resp){
if(resp.length>0){
req.flash('error','您输入的用户名已存在!');
res.redirect('/user/reg');
}else{
knex('user').where('email',postUser.email).select().then(function(resp){
if(resp.length){
req.flash('error','您输入的邮箱已存在!');
res.redirect('/user/reg');
}else{
knex('user').returning('id').insert({
username:postUser.username,
password:password,
email:postUser.email,
realname:postUser.realname}).then(function(resp){
if(resp[0]){
req.flash('success','注册成功,返回登录!');
res.redirect('/user/reg');
};
})
}
})
}
});
7 回复
async
https://github.com/dead-horse/callback_hell 来这里挑一个库去研究
http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/
用 eventproxy。
把这2句
变成一个 error 事件。每次 ep.emit(‘error’).
然后设个事件 ep.all(‘email_no_concilct’, ‘username_no_conflict’, function () {// do sth})
感谢楼上各位朋友的回复,最终我还是用了knex自带的when.js
虽然拉平了,但是感觉代码依然还是很丑陋 呵呵
@lz http://callbackhell.com/ 看下这篇文章,可以结合一下里面提到的几个小技巧
好的,谢谢