未定义 'callback' 与异步瀑布 / Node.js
Undefined 'callback' with Async Waterfall / Node.js
我正在尝试使用 Async 在 Node.js 中运行瀑布,但我一直收到错误
callback (null, articleDataSafe, req);
^
TypeError: undefined is not a function
代码如下
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
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);
},
…
但是我怀疑你根本不需要异步。
我正在尝试使用 Async 在 Node.js 中运行瀑布,但我一直收到错误
callback (null, articleDataSafe, req); ^ TypeError: undefined is not a function
代码如下
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
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);
},
…
但是我怀疑你根本不需要异步。