gpt4 book ai didi

node.js - Mongoose 和 async.parallel

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:22 25 4
gpt4 key购买 nike

我想要获得如下所示的对象,以便将它们传递到我的 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' } ] }

这是我在 Controller 中的代码,用于使用 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);
});


});

最佳答案

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

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

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

关于node.js - Mongoose 和 async.parallel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com