0

I am trying to get a waterfall working in Node.js using Async but I keep getting the error

callback (null, articleDataSafe, req); ^ TypeError: undefined is not a function

The code is as follows

async.waterfall([
                        function sanitizeData (articleData, req, callback) {

                                articleDataSafe = sanitizeArticle(articleData);
                                console.log('s1');
                                callback (null, articleDataSafe, req);

                        },

                        function _validateData (articleDataSafe, req, callback) {

                               var errors = validateArticle(articleData);
                               var err = null;
                               if(errors.length > 0){
                                    // return error messages back to the browser
                                    err = JSON.stringify({'error': errors, "message": "fail"});
                                 };
                                console.log('s2');

                               callback (err, articleDataSafe, req);

                        },

                        function _saveArticle (articleDataSafe, req, callback) {

                                // work out the tags hash
                                var tags = articleDataSafe['tags'];
                                var tagsArray = tags.split(",");
                                tagsArray.sort();
                                var tagsString = tagsArray.join(); 
                                var hashedTags = sha512(tagsString);
                                articleDataSafe['hashedTags'] = hashedTags;

                                // then save the articles
                                var savedArticle = saveArticle(req, articleDataSafe);

                                console.log('s3');

                                if(!savedArticle){
                                    var err = JSON.stringify({'error': 'notSaveArticle', 'message': 'fail'});
                                }

                                callback (err, articleDataSafe);

                        },

                        function _saveTags (articleDataSafe, callback) {

                                var tagsDone = saveTags(articleDataSafe);

                                if(tagsDone.length > 0){
                                    // return error messages back to the browser
                                    var err = JSON.stringify({'error': tagsDone, "message": "fail"});
                                 };
                                console.log('s4');

                                callback (err, articleDataSafe);

                        },

                        function _saveTagSets (articleDataSafe, callback) {

                                var tagSetsDone = saveTagSets(data);

                                if(tagSetsDone.length > 0){
                                    // return error messages back to the browser
                                    var err = JSON.stringify({'error': errors, "message": "fail"});
                                 };
                                console.log('s5');

                                callback (err, articleDataSafe);

                        }
                    ], function (error, success) {

                        var responseMessage = {'error': '', "message": "success"};

                        if (error) { 
                            responseMessage = error; 
                        } 

                        res.end(JSON.stringify(responseMessage));

                    });

Can any explain what is going wrong. I have been trying for ages now to get it working with no luck.

Any help will be appreciated.

Thanks, Cs1h

1
  • Wait, why are you using async.waterfall at all when you have nothing asynchronous to do? Commented Feb 3, 2015 at 21:15

1 Answer 1

3

The first function is passed no other argument than the callback. You'd need to start like this:

async.waterfall([
    function sanitizeData (callback) {
//                        ^^^^^^^^^^ no articleData, no req
        articleDataSafe = sanitizeArticle(articleData);
        console.log('s1');
        callback (null, articleDataSafe, req);
    },
    …

However I doubt you need async at all.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bergi, that seems to work. I will need async later on for more complex matters but I wanted to use with something more simple first.
Thanks @Bergi . This solved a bugging careless mistake i was sitting on

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.