CNode

请教这段数据库代码中的回调该怎么拉平

Ggastrodia发布于12 年前最后回复12 年前7 回复5344 浏览0 收藏
        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)

C
chloe#1·12 年前

async

D
dead-horse#2·12 年前

https://github.com/dead-horse/callback_hell 来这里挑一个库去研究

A
alsotang#4·12 年前

用 eventproxy。

把这2句

                        req.flash('error','您输入的邮箱已存在!');
                        res.redirect('/user/reg');

变成一个 error 事件。每次 ep.emit('error').

  1. 查询重复 username 和 email 的语句让它们并行。

然后设个事件 ep.all('email_no_concilct', 'username_no_conflict', function () {// do sth})

  1. 总的来说,就是每一个逻辑块,都把它抽象成一个或多个事件的发生,并通过监听和抛出来使之扁平。
var ep = new eventproxy();

ep.on('confilict', function (msg) {
  req.flash('error', msg);
  res.redirect('/user/reg');
});

knex('user').where('username',postUser.username).select().then(function(resp){
  if(resp.length>0){
    return ep.emit('confilict', '您输入的用户名已存在!');
  }
  ep.emit('username_ok');
});

knex('user').where('email',postUser.email).select().then(function(resp){
  if(resp.length){
    return ep.emit('confilict', '您输入的邮箱已存在!');
  }
  ep.emit('email_ok');
});

ep.all('email_ok', 'username_ok', function (email, username) {
  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');
      }
    });
});

G
gastrodia#5·12 年前

感谢楼上各位朋友的回复,最终我还是用了knex自带的when.js

        knex('user').where('username',postUser.username).select()
            .then(function(resp){
                if(resp.length>0){
                    return when.reject({info:'您输入的用户名已存在!',redirect:'/user/reg'});
                }else{
                    return knex('user').where('email',postUser.email).select()
                }
            })
            .then(function(resp){
                if(resp.length>0){
                    return when.reject({info:'您输入的邮箱已存在!',redirect:'/user/reg'});
                }else{
                    return 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');
                };
            })
            .otherwise(function(error){
                if(error){
                    req.flash('error',error.info);
                    res.redirect(error.redirect);
                }
            })

虽然拉平了,但是感觉代码依然还是很丑陋 呵呵

D
dead-horse#6·12 年前

@lz http://callbackhell.com/ 看下这篇文章,可以结合一下里面提到的几个小技巧

参与回复
登录后即可参与回复。登录