使用Async Waterfall / Node.js进行未定义的“回调”

时间:2015-02-03 21:13:49

标签: javascript node.js async.js waterfall

我正在尝试使用Async在Node.js中运行瀑布但我不断收到错误

  

回调(null,articleDataSafe,req);                                                                   ^   TypeError:undefined不是函数

代码如下

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));

                    });

可以解释出现了什么问题。我已经尝试了很长时间才能让它运转起来没有运气。

任何帮助将不胜感激。

谢谢, CS1H

1 个答案:

答案 0 :(得分:3)

The first function is passed no other argument than the callback。你需要这样开始:

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

但是我怀疑你需要异步。