我正在尝试从函数getFeed(),feedItems和feedMeta返回值。我收到以下内容:
/Users/react-backend/node_modules/async/dist/async.js:228
return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction';
^
TypeError: Cannot read property 'Symbol(Symbol.toStringTag)' of undefined
at isAsync (/Users/react-backend/node_modules/async/dist/async.js:228:32)
at wrapAsync (/Users/react-backend/node_modules/async/dist/async.js:232:12)
at /Users/react-backend/node_modules/async/dist/async.js:3866:9
at replenish (/Users/react-backend/node_modules/async/dist/async.js:998:17)
at /Users/react-backend/node_modules/async/dist/async.js:1002:9
at eachOfLimit (/Users/react-backend/node_modules/async/dist/async.js:1027:24)
at /Users/react-backend/node_modules/async/dist/async.js:1032:16
at _parallel (/Users/react-backend/node_modules/async/dist/async.js:3865:5)
at Object.series (/Users/react-backend/node_modules/async/dist/async.js:4721:5)
at Object.<anonymous> (/Users/react-backend/feedParser2.js:114:7)
at Module._compile (module.js:662:30)
at Object.Module._extensions..js (module.js:673:10)
at Module.load (module.js:575:32)
at tryModuleLoad (module.js:515:12)
at Function.Module._load (module.js:507:3)
at Function.Module.runMain (module.js:703:10)
使用下面的代码时,在控制台中:
function getFeed(callback) {
let req = request(urlTestFeed);
let feedparser = new FeedParser(feedParserOptions);
let feedItems = [];
let feedMeta = null;
req.on('response', function(response) {
let stream = this;
if (response.statusCode == 200) {
stream.pipe(feedparser);
}
});
req.on('error', function(err) {
console.log('getFeed: err.message == ' + err.message);
});
feedparser.on('meta', function() {
try {
feedMeta = this.meta;
} catch (err) {
console.log('getFeed: err.message == ' + err.message);
}
});
feedparser.on('readable', function() {
try {
let item = this.read();
if (item !== null) {
feedItems.push(item);
}
} catch (err) {
console.log('getFeed: err.message == ' + err.message);
}
});
feedparser.on('end', function() {
callback(undefined, feedItems, feedMeta);
});
feedparser.on('error', function(err) {
console.log('getFeed: err.message == ' + err.message);
callback(err);
});
}
异步:
async.waterfall([
getFeed(function(err, feedItems, feedMeta) {
console.log(feedMeta)
if (!err) {
if (feedMeta.title && feedMeta.description && feedMeta.link) {
}
}
})]);
如果我将callback(undefined, feedItems, feedMeta);
添加到getFeed中的最后一行:
function getFeed(callback) {
let req = request(urlTestFeed);
let feedparser = new FeedParser(feedParserOptions);
let feedItems = [];
let feedMeta = null;
req.on('response', function(response) {
let stream = this;
if (response.statusCode == 200) {
stream.pipe(feedparser);
}
});
req.on('error', function(err) {
console.log('getFeed: err.message == ' + err.message);
});
feedparser.on('meta', function() {
try {
feedMeta = this.meta;
} catch (err) {
console.log('getFeed: err.message == ' + err.message);
}
});
feedparser.on('readable', function() {
try {
let item = this.read();
if (item !== null) {
feedItems.push(item);
}
} catch (err) {
console.log('getFeed: err.message == ' + err.message);
}
});
feedparser.on('end', function() {
callback(undefined, feedItems, feedMeta);
});
feedparser.on('error', function(err) {
console.log('getFeed: err.message == ' + err.message);
callback(err);
});
callback(undefined, feedItems, feedMeta);
}
我在控制台中收到以下内容:
null
/Users/react-backend/feedParser2.js:119
if (feedMeta.title && feedMeta.description && feedMeta.link) {
^
TypeError: Cannot read property 'title' of null
添加回调时feedMeta的值发生了什么变化?如何将feedparser,feeditems和err中的值传递给Async.waterfall?
答案 0 :(得分:1)
猜测这个太少,太晚了 - 但问题并不是getFeed没有执行(它实际上正在执行 - 但是你在尝试之前得到的结果还没有回来从URL读取)。问题在于您将异步模式与同步模式混合在一起。
我建议在将其移入异步瀑布之前简化您尝试做的事情。
具体来说,将readFeed的内容改为:
function getFeed(callback){
console.log('getFeed');
return callback(null, [], {});
}
然后使用以下
调用它async.waterfall([
function first (next){
getFeed(function(err, feedItems, feedMeta) {
console.log('getFeed cb:', feedMeta);
if (!err) {
if (feedMeta.title && feedMeta.description && feedMeta.link) {
}
}
return next(err);
})
}
],
function(err){
console.log('waterfall done: err:',err);
});
以下是瀑布流量的一个例子&#34; (即,给瀑布提供的功能不止一个):
async.waterfall([
function first (next){
getFeed(function(err, feedItems, feedMeta) {
console.log('getFeed cb:', feedMeta);
if (!err) {
if (feedMeta.title && feedMeta.description && feedMeta.link) {
}
}
return next(null, "rando");
})
},
function second(input, next){
console.log('second: input:', input);
return next('something bad happened');
}
],
function(err){
console.log('waterfall done: err:',err);
});