javascript – 无法迭代数组并保存到mongoose.回调问题?

前端之家收集整理的这篇文章主要介绍了javascript – 无法迭代数组并保存到mongoose.回调问题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在学习节点,表达,mongo,以及在此过程中,javascript.我正在尝试使用RSSparser获取一个功能,获取故事列表并将其保存到mongoose的mongo数据库中.

我已经完成了RSS工作,而且我正在重复这些故事,这是我遇到问题的拯救.我想1)检查数据库中是否存在故事,2)如果没有,请保存.我想我在处理回调的过程中迷失了方向.这是我当前的代码,带有注释.

  1. RSSparser.parseURL(url,options,function(err,out){
  2. // out.items is an array of the items pulled
  3. var items = out.items;
  4. var story;
  5. for (var i=0; i

当这个运行时,我看到的是很多控制台输出

它开始显示所有故事(许多省略):

  1. items[i] is :TSA Drops Plan to Let Passengers Carry Small Knives on Planes
  2. story title is : TSA Drops Plan to Let Passengers Carry Small Knives on Planes
  3. items[i] is :BUILDING COLLAPSE:1 Reportedly Dead,13 Pulled From Philly Rubble
  4. story title is : BUILDING COLLAPSE:1 Reportedly Dead,13 Pulled From Philly Rubble
  5. items[i] is :CONTROVERSIAL PAST: Obama's UN Nominee Once Likened US 'Sins' to Nazis'
  6. story title is : CONTROVERSIAL PAST: Obama's UN Nominee Once Likened US 'Sins' to Nazis'
  7. items[i] is :WRITING OUT WRIGHTS: Bill Gives First Powered Flight Nod to Whitehead
  8. story title is : WRITING OUT WRIGHTS: Bill Gives First Powered Flight Nod to Whitehead
  9. items[i] is :BREAKING NEWS: Rice Named to Top Security Post Despite Libya Fallout
  10. story title is : BREAKING NEWS: Rice Named to Top Security Post Despite Libya Fallout

然后继续像(许多省略):

  1. row: null
  2. about to save story.title: Best Ribs in America
  3. row: null
  4. about to save story.title: Best Ribs in America
  5. row: null
  6. about to save story.title: Best Ribs in America
  7. row: null
  8. about to save story.title: Best Ribs in America
  9. row: null
  10. about to save story.title: Best Ribs in America
  11. row: null
  12. about to save story.title: Best Ribs in America
  13. row: { title: 'Best Ribs in America',url: 'http://www.foxnews.com/leisure/2013/06/05/10-best-ribs-in-america/',published: 1370463800000,_id: 51af9f881995d40425000023,__v: 0 }

它重复了“即将保存”标题(这是Feed中的最后一个故事),并且它保存了一次故事,就像最后一行所示.

console.log输出显示我把它,所有的故事标题输出在顶部,然后从底部的query.exec()调用内的所有东西.

任何帮助表示赞赏……

最佳答案
这个问题是exec回调中引用的故事将被设置为for循环中迭代的最后一件事,一旦回调将被执行,因为所有执行的函数都引用了相同的实例.变量.

解决这个问题的最简单方法是简单地将for循环中的每个东西包装在您使用参数立即执行的函数中,如:

  1. RSSparser.parseURL(url,out){
  2. // out.items is an array of the items pulled
  3. var items = out.items;
  4. for (var i=0; i

我没有测试过这个,但我相信你会发现它会解决你的问题

另一个更简单,更清晰,更好的方法是迭代数组上forEach循环中的项目,如果你的平台支持(哪个node.js) – 这个版本更漂亮:

  1. RSSparser.parseURL(url,out){
  2. // out.items is an array of the items pulled
  3. out.items.forEach(function(item) {
  4. //create a mongoose story
  5. var story = new schemas.Stories({
  6. title: item.title,published: item.published_at
  7. });
  8. // setup query to see if it's already in db
  9. var query = schemas.Stories.findOne({
  10. "title" : story.title,"url" : story.url
  11. });
  12. //execute the query
  13. query.exec( function(err,so save
  14. console.log('about to save story.title: ' + story.title);
  15. story.save(function (err){
  16. console.log("error in save: " + err);
  17. });
  18. }
  19. });
  20. });
  21. });

猜你在找的JavaScript相关文章