简体   繁体   中英

Can I run a Sails.js Service synchronously?

I have a Sails.js Service that parses an RSS feed using the FeedParser package. The service takes a single RSS feed URL, parses it, creates an array of posts and returns the array. 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 . How do I make it so that the script waits until the RssService returns posts before I console log?

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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