Node.js - 无效的select()参数

时间:2012-12-24 01:44:17

标签: javascript node.js mongodb mongoose

我正在尝试创建一个列表来显示数据库中的所有teams。我收到错误TypeError: Invalid select() argument. Must be a string or object.任何人都可以帮我弄清楚如何解决这个问题?我只是想用它来打印当前团队的列表,然后可以点击这个团队列表来编辑团队名称!

所以在我的Schema Team我有这个:

Team.statics.getAllMeta = function(cb){
  var query = this.find({}, ['key', 'name'], cb);
  return query.exec(cb);
};

然后在我的index.js routes我有这个:

var getAllMeta = function(req, res, next){
  Team.getAllMeta(function(err, teamsList){
    if(!err && teamsList){
      req.teamsList = teamsList;
    }
    next(err);
  });
};

然后,我可以在routes我的team页面中调用这样的内容:

  app.get('/team', getAllMeta, function(req, res){
    util.log('Serving request for url[GET] ' + req.route.path);
    res.render('team', {'teamsList' : req.teamsList});
  });

此处仅供参考,index.js来自routes

的其他相关部分
  app.get('/', function(req, res){
    logger.log('Serving request for url [GET]' + req.route.path)
    Team.getAll(function(err, allTeams){
      if(!err && allTeams){
        res.render('index', {'allTeams' : allTeams});
      }else{
        util.log('Error fetching team from database : ' + err);
        res.render('error');
      }
    });
  });

  app.get('/show/team/:key', function(req,res){
    Team.findByKey(req.params.key, function(err, teamData){
      if(!err && teamData){
        teamData = teamData[0];
          res.json({
            'retStatus' : 'success',
            'teamData' : teamData
          });
      } else {
        util.log('Error in fetchin Team by key: ' + req.params.key);
        res.json({
          'retStatus' : 'failure',
          'msg' : 'Error in fetching Team by key: ' + req.params.key
        });
      }
    });
  });

1 个答案:

答案 0 :(得分:1)

尝试

var query = this.find({}, 'key name', cb);

https://github.com/LearnBoost/mongoose/issues/1087