insertMany不是一个函数

对于我的项目,我正在制作一个让用户创build和configuration自己的足球锦标赛的系统。 在锦标赛结束之后,我必须把poules和team放入锦标赛的文件中。 我不断收到以下错误:

TypeError: toernooi.insertMany is not a function at C:\xampp\htdocs\nodeprojects\contactlistapp\server.js:254:13 at Query.<anonymous> (C:\xampp\htdocs\nodeprojects\contactlistapp\node_modules\mongoose\lib\model.js:3398:16) at C:\xampp\htdocs\nodeprojects\contactlistapp\node_modules\kareem\index.js:259:21 at C:\xampp\htdocs\nodeprojects\contactlistapp\node_modules\kareem\index.js:127:16 at nextTickCallbackWith0Args (node.js:420:9) at process._tickCallback (node.js:349:13) 

我试图用下面的代码在MongoDB中将数组插入到现有的文档中:

 app.put('/poules/:id', function(req,res){ //Select Toernooi object in DB by ID Toernooi.findById(req.params.id, function(err, toernooi){ if(err) res.send(err); toernooi.poules = { poule : { team: { teamnaam: 'psv', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'ajax', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'feyenoord', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'ado', pt: '0', dptplus: '0', dptminus: '0' } }, poule : { team: { teamnaam: 'vitesse', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'achilles', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'jvc', pt: '0', dptplus: '0', dptminus: '0' }, team: { teamnaam: 'twente', pt: '0', dptplus: '0', dptminus: '0' } } }; toernooi.insertMany(toernooi.poules, function(error, docs)); }); 

我无法弄清楚什么是错误的,因为我是NodeJS和Mongo的新手,我想我可能会做一些简单的非常错误的事情。

Mongoose版本:4.7 MongoDB版本:3.2.10

在你给的例子中, toernooi是你最高查询的结果。 结果没有insertMany的function,只有模型Toernooi

在任何情况下,你在这里做的是一个文档的简单更新,所以下面的工作:

 app.put('/poules/:id', function(req,res){ //Select Toernooi object in DB by ID Toernooi.findById(req.params.id, function(err, toernooi){ if(err) res.send(err); toernooi.poules = [ [ { teamnaam: 'psv', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'ajax', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'feyenoord', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'ado', pt: '0', dptplus: '0', dptminus: '0' } ],[ { teamnaam: 'vitesse', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'achilles', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'jvc', pt: '0', dptplus: '0', dptminus: '0' }, { teamnaam: 'twente', pt: '0', dptplus: '0', dptminus: '0' } ] ]; toernooi.save(function(err,res){ }); }); 

编辑:正如在评论中提到的,你的语法是无效的皮鞋。 这里是一个数组。