Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于检索论坛作者列表的Sailjs代码无效_Javascript_Node.js_Nested_Sails.js - Fatal编程技术网

Javascript 用于检索论坛作者列表的Sailjs代码无效

Javascript 用于检索论坛作者列表的Sailjs代码无效,javascript,node.js,nested,sails.js,Javascript,Node.js,Nested,Sails.js,我正在用SailsJs写一个讨论板,我有一个主题,里面有一个问题和一堆回复。现在回复由不同的用户进行,而replyObject只包含authorId。我还需要为每个回复显示authorName。根据我有限的理解,我编写了以下控制器: 'showTopic': function(req, res, next) { if(req.param('id')!=null) { Topic.findOne(req.param('id'), function foundTopic(err, topic

我正在用SailsJs写一个讨论板,我有一个
主题
,里面有一个问题和一堆回复。现在回复由不同的用户进行,而
replyObject
只包含
authorId
。我还需要为每个回复显示
authorName
。根据我有限的理解,我编写了以下控制器:

'showTopic': function(req, res, next) {
if(req.param('id')!=null) {
    Topic.findOne(req.param('id'), function foundTopic(err, topic) {
        Reply.find({
          topicId: topic.id
        }).done(function(err, replies) {
          if (err) return next(err);
          if (!replies) return next();         

          var authorIds = [];
          var authorList = [];

          for(var reply =0; reply<replies.length; reply++) {
            var authorId = replies[reply].authorId;
            if(authorIds.indexOf(authorId) === -1) {
              authorIds.push(authorId);
              User.findOne(authorId , function foundUser(err, author) { 
               if (err) return next(err);
               authorList.push(author);               
             });
            }
          };

          res.view({
            topic: topic,
            replies: replies,
            authors: authorList
          });
     });
}
'showTopic':函数(req、res、next){
if(请求参数('id')!=null){
Topic.findOne(请求参数('id'),函数foundTopic(err,Topic){
答复.查找({
topicId:topic.id
}).done(功能(错误、回复){
if(err)返回next(err);
如果(!回复)返回下一步();
var-authorIds=[];
var authorList=[];

对于(var reply=0;reply我从未使用过EventProxy,但在快速查看之后,我认为这不是您需要的,因为它涉及(毫不奇怪)事件,您希望简化非基于事件的嵌套异步回调。我建议改为使用,这样您可以将上面的内容重写为:

'showTopic': function(req, res) {

  if (req.param('id') == null) {
      return res.send("No topic ID sent!");
  }

  var authorList = [];
  async.auto({

     // Find the topic, and send it as an argument to the callback
     topic: function(cb) {Topic.findOne(req.param('id')).exec(cb);},

     // Find the replies, in parallel to the above
     replies: function(cb) {Reply.find({topic: req.param('id')}).exec(cb);},

     // After "replies" is done, construct the author list
     authors: ['replies', function(cb, results) {

        // async.auto stores the results of everything that ran before
        var replies = results.replies;

        // Get the unique list of authors using Lodash (you could write
        // this manually if you don't want to include the Lodash library!)
        var authorIds = _.unique(_.pluck(replies, 'authorId'));

        // Find all the authors with those IDs
        User.find({id: authorIds}).exec(cb);

     }]

  },

     // The second argument to async.auto is called when all of the
     // tasks complete, or if any of the callbacks returns an error
     function finished (err, results) {

        if (err) {return res.serverError(err);}
        res.view({
          topic: results.topic,
          replies: results.replies,
          authors: results.authors
        });

     } 

  );

}
有关更多信息,请参阅


注意事项:您需要
npm安装
并且
需要
异步库和Lodash库才能使上述功能正常工作(尽管您可以将其重写为不使用Lodash,如果您愿意的话)。此外,您几乎不需要使用(甚至声明)
next
在您的Sails控制器中。控制器应该是中间件链中的最后一站,除非在极少数情况下,如果您发现自己想使用
next
,您可能可以用它来解决问题。

谢谢您的回复!我刚刚开始使用node,所以我支持一些好的实践我需要学习的姿势。我会尝试一下这个方法,看看这是否适合我的目的。