如何运行有限的猫鼬验证

时间:2015-08-23 12:26:23

标签: node.js mongoose

尝试编写新的数据库条目,突然间,当我尝试保存条目时,我开始收到以下错误消息。

/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/lib/schema/documentarray.js:100
      doc.validate(function (err) {
          ^
TypeError: undefined is not a function

我无法理解的是条目的哪一部分导致了这个问题。由于以前的代码问题,我设置了逐步完成从前端发送的数据的所有部分。它就在那里,这就是我正在使用的架构。

var  complexSchema = mongoose.Schema({
    _creatorId : { type: ObjectId, ref: 'User'},
    name: String,
    category: { type:ObjectId, ref: 'Categories'},
    sections : [{
        name: String,
        text: String,
        closureType: Number
    }]
});

代码:

var comp = new betComplex();
for (var i in req.body){
    console.log("ID: " + i +" DATA: " + req.body[i])
    comp[i] = [];

}
for (var i in req.body.sections){
    console.log("ID: " + i +" DATA: " + req.body.sections[i])
    comp.sections[i] = req.body.sections[i];
    for ( var b in req.body.sections[i]){
        console.log("MEMBER: " + b + " DATA: " + req.body.sections[i][b])
        comp.sections[i][b] = req.body.sections[i][b];
    }
}
comp.save( function(err) {
    if(err) {
        console.log(err)
        return res.status(500).send(err);
    }
    return res.send(comp._id);
})

问题在于我无法找到一种简单直接的方法来确定导致此问题的架构(或传入数据)的哪一部分。

进入它的数据

id: 0
name: "This is the main name"
sections: {0: {name: "Name of Section 0", text: "<p>Text for section 0</p>↵", closureType: 0},…}
0: {name: "Name of Section 0", text: "<p>Text for section 0</p>↵", closureType: 0}
closureType: 0
name: "Name of Section 0"
text: "<p>Text for section 0</p>↵"
1: {name: "This is the name of section 1", text: "<p>Section 1&nbsp;text</p>↵", closureType: 0}
closureType: 0
name: "This is the name of section 1"
text: "<p>Section 1&nbsp;text</p>↵"
2: {name: "Section 2 name", text: "<p>Section 2 text&nbsp;</p>↵", closureType: 0}
closureType: 0
name: "Section 2 name"
text: "<p>Section 2 text&nbsp;</p>↵"

1 个答案:

答案 0 :(得分:0)

经过大量的调整,我设法找出问题所在。 从来没有设法找到一种平滑的方法来测试验证,因此最终减少了一步一步保存的位,并且一旦我没有保存它工作的部分。

问题是你不能创造&#34;记录中的对象,I.E。在这种情况下的部分,我不能创建一个空的并填充它。所以诀窍就是使用push。这就是我的解决方案的外观。

        for (var i in req.body.sections){
            console.log("ID: " + i +" DATA: " + req.body.sections[i])
            comp.sections.push(req.body.sections[i]);
        }

希望这有助于某人:)