0

Here is an example for querying user from database then if user exists, another query for phone:

module.exports = function (username, req, res, next) {

    var query = User.where('username', new RegExp('^' + username + '$', 'i'));
    query.findOne(function (err, user) {
        if (err) {
            res.send("error");
            //## position D
        } else if (!user) {
            res.send("user not found");
            //## position D
        } else {
            //user exists
            //search for phone
            var query2 = Phone.where('phone', new RegExp('^' + user.phone + '$', 'i'));
            query2.findOne(function (err, phone) {
                if (err) {
                    res.send("error");
                    //## position D
                } else if (!phone) {
                    res.send("phone not found");
                    //## position D
                } else {
                    res.send(phone);
                    //## position D
                }
                //## position C
            });
        }
        //## position B
    });
    //## position A
}

My question is: based on this code, where is the correct place to set return next(); ? and why ?

I set some proposals:

  • Position A: @ the bottom of function. here it would be called even earlier than querying data from database

  • Position B: @ the bottom of 1st callback function

  • Position C: @ the bottom of last callback function

  • Position D: after res.send(..) immediately

P.S: I know that I can pass error as object inside next.. I just want to make code simple to focus on the main issue.

Thanks yokefellows,'.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • Well if you send a response, you wouldn't need to use `next` at all? – Bergi Jun 07 '15 at 22:52
  • 1
    In Restify Doc:"You are responsible for calling next() in order to run the next handler in the chain. As below, you can pass an Error object in to have restify automatically return responses to the client." http://mcavage.me/node-restify/ ... so its recommended to use even with res.send .. but where? – Maher Abuthraa Jun 07 '15 at 23:00
  • Ah, right, thanks for the link. I had expected it would only be needed if you wanted to continue the chain, but it seem also be required to call `next(false)` if you want to short-circuit. Probably restify needs to do some cleanup. – Bergi Jun 08 '15 at 00:19

1 Answers1

1

Position D, definitely, but you can have something "cleaner" with callbacks.

module.exports = function (username, req, res, next) {

    var query = User.where('username', new RegExp('^' + username + '$', 'i'));
    query.findOne(function (err, user) {
        if (err) {
            return next(err);
        }
        if (!user) {
          res.send("user not found");
          return next();
        }
        //user exists
        //search for phone
        var query2 = Phone.where('phone', new RegExp('^' + user.phone + '$', 'i'));
        query2.findOne(function (err, phone) {
            if (err) {
                return next(err);
            }
            if (!phone) {
                res.send("phone not found");
            } else {
                res.send(phone);
            }
            next();
        });
    });
}
Pierre Inglebert
  • 3,858
  • 1
  • 18
  • 22
  • Thanks.. you code is correct .. I just get used to use return always to prevent falling in such as case that after calling next I use res.send or next again which will be a painful issue. but with D .. I agree with you ..I just wanted to be sure – Maher Abuthraa Jun 07 '15 at 23:31