简体   繁体   English

我可以同步运行Sails.js服务吗?

[英]Can I run a Sails.js Service synchronously?

I have a Sails.js Service that parses an RSS feed using the FeedParser package. 我有一个Sails.js服务,该服务使用FeedParser包解析RSS提要。 The service takes a single RSS feed URL, parses it, creates an array of posts and returns the array. 该服务采用单个RSS feed URL,对其进行解析,创建一个帖子数组并返回该数组。 In my controller, this is what I have: 在我的控制器中,这就是我所拥有的:

var stories = RssService.fetch(rssLinks[0]);
console.log(stories);

res.json({ stories: stories }, 200);

However, it looks like this is an asynchronous request and as a result, the console log says undefined and the response inside the JSON is null . 但是,这看起来像是一个异步请求,因此,控制台日志显示undefined并且JSON内的响应为null How do I make it so that the script waits until the RssService returns posts before I console log? 如何使脚本等待RssService返回帖子,然后再控制台日志?

Thank you. 谢谢。

Put a callback into your service. 在您的服务中添加回调。 View comments above. 查看上面的评论。

Controller 调节器

RssService.fetch(rssLinks[0], function(err,data){

  if(err) return next(err)
  return res.json({stories:data}, 200);

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM