1

I am new to node.js and trying to create a small API. I am using Express for routing and async.jc for processing the Calls Step by Step. This works fine when only one Request comes in, but when i try to fire 2 or more requests from my Machine the process is mixing up values in variables and crashes.

Here is short snippet from my source:

router.get('/new', function (request, res) {
    async.waterfall([
        function(callback) {

            // get a unique domain
            client.spop('domainSet', function(err, domain) {
                userdata = {
                    email: 'user' + '@' + domain + '.' + mydomain,
                    password: randomstring.generate(10),
                    username: domain,
                    name: domain,
                    confirm: "false"
                }
                if (err) return next(err);
                callback(null, userdata, userdata.password);
            })

        },
        function(userdata, userpwd, callback) {
            myapp.users.create(userdata, function(user) {
                console.log('Creating new user: ' + userdata.email);
                callback(null, user, userpwd);
            });
        },

How is it possible to keep the "context/scope" per request? i am sure im missing something (-: Or is the only problem that i fire the request from the same Machine??

2
  • userdata in the first function is not declared anywhere, so it is getting global scope. try var userdata = { Commented Jun 6, 2016 at 0:49
  • ahhh ;) Thanks - if you create a answer from your comment i will accept it ;) Commented Jun 6, 2016 at 5:16

1 Answer 1

2

userdata in the first waterfall function needs to be declared within that function, otherwise it has global scope.

router.get('/new', function (request, res) {
    async.waterfall([
        function(callback) {

            // get a unique domain
            client.spop('domainSet', function(err, domain) {
                var userdata = {
                    email: 'user' + '@' + domain + '.' + mydomain,
                    password: randomstring.generate(10),
                    username: domain,
                    name: domain,
                    confirm: "false"
                }
                if (err) return next(err);
                callback(null, userdata, userdata.password);
            })

        },
        function(userdata, userpwd, callback) {
            myapp.users.create(userdata, function(user) {
                console.log('Creating new user: ' + userdata.email);
                callback(null, user, userpwd);
            });
        },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.