1

我想得到一个像下面这样的对象,这样我就可以将它们传递给我的 Dust.js 模板。

{ officers:
   [ { _id: '543dcbcf846c139cc042c813', name: 'Alpha' },
     { _id: '543dcc25846c139cc042c814', name: 'Beta' },
     { _id: '543dcd91846c139cc042c815', name: 'Gamma' } ],
  branches:
   [ { _id: '543dcbcf846c139cc042c813', name: 'Alpha' },
     { _id: '543dcc25846c139cc042c814', name: 'Beta' },
     { _id: '543dcd91846c139cc042c815', name: 'Gamma' } ] }

这是我在控制器中使用 Mongoose 获取文档并将它们组合起来的代码。我总是收到错误 500,我的代码有什么问题吗?

var Officers = require('../../models/officers');
var Branches = require('../../models/branches');

router.get('/create', function (req, res) {


  async.parallel({
    officers: function(cb){
      Officers.find({}, '_id name', cb);
    },
    branches: function(cb){
      Branches.find({}, '_id name', cb);
    }
  }, function(err, results){
    res.render('companies/create', results);
  });


});
4

1 回答 1

0

我上面的代码没有任何问题。原来我忘了把它放在var async = require('async');代码之前。

var Officers = require('../../models/officers');
var Branches = require('../../models/branches');
var async = require('async');

router.get('/create', function (req, res) {
...
...
于 2014-10-18T14:35:27.810 回答