【问题标题】:insertMany is not a functioninsertMany 不是函数
【发布时间】:2016-11-30 19:09:44
【问题描述】:

对于我的项目,我正在制作一个系统,让用户可以创建和配置他们自己的足球锦标赛。比赛结束后,我必须将 poules 和球队放入 mongo 的比赛文件中。我不断收到以下错误:

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 的新手,我想我可能做一些简单的非常错误的事情。

猫鼬版本:4.7 MongoDB 版本:3.2.10

【问题讨论】:

  • 您的 toernooi.poules 声明语法无效。对象不能具有具有相同键的多个属性。相反,您可能需要数组。
  • 请使用该链接您可以找到详细信息:stackoverflow.com/questions/37697448/…

标签: javascript node.js mongodb mongoose


【解决方案1】:

在您给出的示例中,toernooi 是您的顶级查询的结果。结果没有 insertMany 函数,只有模型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){

    });
});

编辑:如 cmets 中所述,您的语法对 poules 无效。这里是一个数组。

【讨论】:

  • 你为什么不使用一个poules数组?不要这样编号。
  • 考虑到以后的查询,锦标赛通常有固定数量的 poules 等,基本上,只是想多了!
  • 现在好多了:D
  • 感谢所有反馈,此代码确实有效,但是现在我在 mongo 中有一个 poules 数组,其中只有一个 ID,它不显示团队或 poules。我想我误解了一些事情
  • 检查您的模型,确保它反映了您想要对结构进行的更改。 'array of arrays' 可能不是最好的设计,但至少现在你知道如何.save() 你的更改了! :)
猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2019-03-16
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多