0

Is the following the correct way to go about things? This is a sign up controller action. Im creating a user and a group to add the user to. Notice I have method level variables called user and group. The rest of the code is asynchronous using the Q module.

Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time?

exports.postSignUp = function(req, res, next) {

    var user,
        group;

    return Q.invoke(exports, 'parseUser', req, null)
        .then(function(u)
        {
            user = u;
            return Q.invoke(exports, 'postCreateUser', user).fail(function(err) { throw err; });
        })
        .then(function()
        {
            group = new Group();
            group.userIds = [user._id];
            return Q.invoke(exports, 'postCreateGroup', group).fail(function(err) { throw err; });
        })
        .then(function()
        {
            user.groupId = group._id;
            group.userIds = [ user._id ];
        })
        .then(function()
        {
            return Q.ninvoke(req, 'login', user).fail(function(err) { throw err; });
        })
        .then(function()
        {
            return res.redirect('/tour');
        })
        .fail(function (err)
        {
            console.log('u:' + user);
            console.log('g:' + group);
            return exports.createValidationError(error, req, res, user);
        });
};
user3428172
  • 147
  • 2
  • 6
  • Didn't that work for you? What exactly is your question? – thefourtheye Mar 22 '14 at 04:19
  • It worked perfectly. My questions are: 1. is it ok to have var user, group on top outside of the "Q". Will the next request to the controller impact those variables? 2. Do I really need to break down all the different steps into different functions or can I just have 1 async function? – user3428172 Mar 22 '14 at 04:59
  • Notice that `.fail(function(err) { throw err; })` can be omitted – Bergi Mar 22 '14 at 14:47

1 Answers1

0

Is it ok to have the method level variables or will they be overriden by the another person signing up at the same time?

No, they won't as they are locally scoped to your postSignUp function.

It is however a little ugly, nesting them might be the better choice here:

exports.postSignUp = function(req, res, next) {
    return Q.invoke(exports, 'parseUser', req, null).then(function(user) {
        return Q.invoke(exports, 'postCreateUser', user).then(function(createdUser) {
            var group = new Group();
            group.userIds = [user._id];
            return Q.invoke(exports, 'postCreateGroup', group).then(function(createdGroup) {
                user.groupId = group._id;
                group.userIds = [ user._id ]; // repeat this?

                return Q.ninvoke(req, 'login', user)
            }).then(function(loggedInuser) {
                return res.redirect('/tour');
            }).fail(function(err) {
               console.log('g:' + group);
               throw err;
            });
        }).fail(function(err) {
            console.log('u:' + user);
            return exports.createValidationError(error, req, res, user);
        });
    }) // .fail(function(err) {
       //     … // parseUser or createValidationError failed
       // });
};

Do I really need to break down all the different steps into different functions or can I just have 1 async function?

You have one async function currently, you might want to break that down into creating a user with a new group, and logging him in plus redirecting him to /tour.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375