object.save()不是函数错误

时间:2016-09-28 19:39:27

标签: javascript node.js mongodb mongoose

我认为问题围绕着使用.save(),并且可能与没有.save()方法的对象有关。

/这是调试器输出(我想也许澄清)/

Debugger listening on port 15454
Server started
events.js:141
      throw er; // Unhandled 'error' event
      ^

TypeError: examBoard.save is not a function
    at /home/ubuntu/workspace/tests/seeds.js:168:23
    at Immediate.Model.$wrapCallback (/home/ubuntu/workspace/tests/node_modules/mongoose/lib/model.js:3336:16)
    at Immediate._onImmediate (/home/ubuntu/workspace/tests/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
    at processImmediate [as _immediateCallback] (timers.js:383:17)


Process exited with code: 1

为了帮助任何人找到实际问题,我使用了examBoad.save();两次(一次创建所有examBoards后一次,接近程序结束时)和question.save();一次,我相信问题与examBoard.save();

有关

var mongoose = require("mongoose");
var examBoard = require("./models/examBoard");
var question = require("./models/question");
var user = require("./models/user");

function seedDB() {
  examBoard.remove({}, function(err) {
    if (err) {
      console.log("Could not remove examBoard data" + err);
    } else {
      examBoard.insertMany({
        name: "AQA",
        modules: [{
          name: "a",
          topics: [{
            name: "a1",
          }, {
            name: "a2",
          }, {
            name: "a3",
          }]
        }, {
          name: "b",
          topics: [{
            name: "b1",
          }, {
            name: "b2",
          }, {
            name: "b3",
          }]
        }, {
          name: "c",
          topics: [{
            name: "c1",
          }, {
            name: "c2",
          }, {
            name: "c3",
          }]
        }]
      }, {
        name: "OCR",
        modules: [{
          name: "a",
          topics: [{
            name: "a1",
          }, {
            name: "a2",
          }, {
            name: "a3",
          }]
        }, {
          name: "b",
          topics: [{
            name: "b1",
          }, {
            name: "b2",
          }, {
            name: "b3",
          }]
        }, {
          name: "c",
          topics: [{
            name: "c1",
          }, {
            name: "c2",
          }, {
            name: "c3",
          }]
        }]
      }, {
        name: "EXL",
        modules: [{
          name: "a",
          topics: [{
            name: "a1",
          }, {
            name: "a2",
          }, {
            name: "a3",
          }]
        }, {
          name: "b",
          topics: [{
            name: "b1",
          }, {
            name: "b2",
          }, {
            name: "b3",
          }]
        }, {
          name: "c",
          topics: [{
            name: "c1",
          }, {
            name: "c2",
          }, {
            name: "c3",
          }]
        }]
      });

      examBoard.save();

      question.insertMany({
        content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1469537053566-3081fd1e0de2'><br><p>This is a bit after the image</p>",
        mark: 6,
        methods: [
          [{
            mark: 1,
            content: "1a"
          }, {
            mark: 2,
            content: "1b"
          }, {
            mark: 3,
            content: "1c"
          }],
          [{
            mark: 1,
            content: "2a"
          }, {
            mark: 2,
            content: "2b"
          }, {
            mark: 3,
            content: "2c"
          }],
          [{
            mark: 1,
            content: "3a"
          }, {
            mark: 2,
            content: "3b"
          }, {
            mark: 3,
            content: "3c"
          }]
        ]
      }, {
        content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1465311440653-ba9b1d9b0f5b'><br><p>This is a bit after the image</p>",
        mark: 9,
        methods: [
          [{
            mark: 2,
            content: "1a"
          }, {
            mark: 3,
            content: "1b"
          }, {
            mark: 4,
            content: "1c"
          }],
          [{
            mark: 2,
            content: "2a"
          }, {
            mark: 3,
            content: "2b"
          }, {
            mark: 4,
            content: "2c"
          }],
          [{
            mark: 2,
            content: "3a"
          }, {
            mark: 3,
            content: "3b"
          }, {
            mark: 4,
            content: "3c"
          }]
        ]
      }, {
        content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1467404899198-ccadbcd96b91'><br><p>This is a bit after the image</p>",
        mark: 12,
        methods: [
          [{
            mark: 3,
            content: "1a"
          }, {
            mark: 4,
            content: "1b"
          }, {
            mark: 5,
            content: "1c"
          }],
          [{
            mark: 3,
            content: "2a"
          }, {
            mark: 4,
            content: "2b"
          }, {
            mark: 5,
            content: "2c"
          }],
          [{
            mark: 3,
            content: "3a"
          }, {
            mark: 4,
            content: "3b"
          }, {
            mark: 5,
            content: "3c"
          }]
        ]
      });

      question.save();

      examBoard[0].module[0].topic[0].push(question[0]);
      examBoard[0].module[0].topic[0].push(question[1]);
      examBoard[0].module[0].topic[0].push(question[2]);
      examBoard[0].module[0].topic[1].push(question[0]);
      examBoard[0].module[0].topic[1].push(question[1]);
      examBoard[0].module[0].topic[1].push(question[2]);
      examBoard[0].module[0].topic[2].push(question[0]);
      examBoard[0].module[0].topic[2].push(question[1]);
      examBoard[0].module[0].topic[2].push(question[2]);
      examBoard[0].module[1].topic[0].push(question[0]);
      examBoard[0].module[1].topic[0].push(question[1]);
      examBoard[0].module[1].topic[0].push(question[2]);
      examBoard[0].module[1].topic[1].push(question[0]);
      examBoard[0].module[1].topic[1].push(question[1]);
      examBoard[0].module[1].topic[1].push(question[2]);
      examBoard[0].module[1].topic[2].push(question[0]);
      examBoard[0].module[1].topic[2].push(question[1]);
      examBoard[0].module[1].topic[2].push(question[2]);
      examBoard[0].module[2].topic[0].push(question[0]);
      examBoard[0].module[2].topic[0].push(question[1]);
      examBoard[0].module[2].topic[0].push(question[2]);
      examBoard[0].module[2].topic[1].push(question[0]);
      examBoard[0].module[2].topic[1].push(question[1]);
      examBoard[0].module[2].topic[1].push(question[2]);
      examBoard[0].module[2].topic[2].push(question[0]);
      examBoard[0].module[2].topic[2].push(question[1]);
      examBoard[0].module[2].topic[2].push(question[2]);
      examBoard[0].module[0].topic[0].push(question[0]);
      examBoard[0].module[0].topic[0].push(question[1]);
      examBoard[0].module[0].topic[0].push(question[2]);
      examBoard[0].module[0].topic[1].push(question[0]);
      examBoard[0].module[0].topic[1].push(question[1]);
      examBoard[0].module[0].topic[1].push(question[2]);
      examBoard[0].module[0].topic[2].push(question[0]);
      examBoard[0].module[0].topic[2].push(question[1]);
      examBoard[0].module[0].topic[2].push(question[2]);
      examBoard[0].module[1].topic[0].push(question[0]);
      examBoard[0].module[1].topic[0].push(question[1]);
      examBoard[0].module[1].topic[0].push(question[2]);
      examBoard[0].module[1].topic[1].push(question[0]);
      examBoard[0].module[1].topic[1].push(question[1]);
      examBoard[0].module[1].topic[1].push(question[2]);
      examBoard[0].module[1].topic[2].push(question[0]);
      examBoard[0].module[1].topic[2].push(question[1]);
      examBoard[0].module[1].topic[2].push(question[2]);
      examBoard[0].module[2].topic[0].push(question[0]);
      examBoard[0].module[2].topic[0].push(question[1]);
      examBoard[0].module[2].topic[0].push(question[2]);
      examBoard[0].module[2].topic[1].push(question[0]);
      examBoard[0].module[2].topic[1].push(question[1]);
      examBoard[0].module[2].topic[1].push(question[2]);
      examBoard[0].module[2].topic[2].push(question[0]);
      examBoard[0].module[2].topic[2].push(question[1]);
      examBoard[0].module[2].topic[2].push(question[2]);

      examBoard[1].module[0].topic[0].push(question[0]);
      examBoard[1].module[0].topic[0].push(question[1]);
      examBoard[1].module[0].topic[0].push(question[2]);
      examBoard[1].module[0].topic[1].push(question[0]);
      examBoard[1].module[0].topic[1].push(question[1]);
      examBoard[1].module[0].topic[1].push(question[2]);
      examBoard[1].module[0].topic[2].push(question[0]);
      examBoard[1].module[0].topic[2].push(question[1]);
      examBoard[1].module[0].topic[2].push(question[2]);
      examBoard[1].module[1].topic[0].push(question[0]);
      examBoard[1].module[1].topic[0].push(question[1]);
      examBoard[1].module[1].topic[0].push(question[2]);
      examBoard[1].module[1].topic[1].push(question[0]);
      examBoard[1].module[1].topic[1].push(question[1]);
      examBoard[1].module[1].topic[1].push(question[2]);
      examBoard[1].module[1].topic[2].push(question[0]);
      examBoard[1].module[1].topic[2].push(question[1]);
      examBoard[1].module[1].topic[2].push(question[2]);
      examBoard[1].module[2].topic[0].push(question[0]);
      examBoard[1].module[2].topic[0].push(question[1]);
      examBoard[1].module[2].topic[0].push(question[2]);
      examBoard[1].module[2].topic[1].push(question[0]);
      examBoard[1].module[2].topic[1].push(question[1]);
      examBoard[1].module[2].topic[1].push(question[2]);
      examBoard[1].module[2].topic[2].push(question[0]);
      examBoard[1].module[2].topic[2].push(question[1]);
      examBoard[1].module[2].topic[2].push(question[2]);

      examBoard[2].module[0].topic[0].push(question[0]);
      examBoard[2].module[0].topic[0].push(question[1]);
      examBoard[2].module[0].topic[0].push(question[2]);
      examBoard[2].module[0].topic[1].push(question[0]);
      examBoard[2].module[0].topic[1].push(question[1]);
      examBoard[2].module[0].topic[1].push(question[2]);
      examBoard[2].module[0].topic[2].push(question[0]);
      examBoard[2].module[0].topic[2].push(question[1]);
      examBoard[2].module[0].topic[2].push(question[2]);
      examBoard[2].module[1].topic[0].push(question[0]);
      examBoard[2].module[1].topic[0].push(question[1]);
      examBoard[2].module[1].topic[0].push(question[2]);
      examBoard[2].module[1].topic[1].push(question[0]);
      examBoard[2].module[1].topic[1].push(question[1]);
      examBoard[2].module[1].topic[1].push(question[2]);
      examBoard[2].module[1].topic[2].push(question[0]);
      examBoard[2].module[1].topic[2].push(question[1]);
      examBoard[2].module[1].topic[2].push(question[2]);
      examBoard[2].module[2].topic[0].push(question[0]);
      examBoard[2].module[2].topic[0].push(question[1]);
      examBoard[2].module[2].topic[0].push(question[2]);
      examBoard[2].module[2].topic[1].push(question[0]);
      examBoard[2].module[2].topic[1].push(question[1]);
      examBoard[2].module[2].topic[1].push(question[2]);
      examBoard[2].module[2].topic[2].push(question[0]);
      examBoard[2].module[2].topic[2].push(question[1]);
      examBoard[2].module[2].topic[2].push(question[2]);

      examBoard.save();
      console.log("Done seeding");
    }
  });
}

module.exports = seedDB;
examBoard模型
var mongoose = require("mongoose");

var topicSchema = new mongoose.Schema({
  name: String,
  questions: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "question"
  }],
});

var moduleSchema = new mongoose.Schema({
  name: String,
  topics: [topicSchema]
});

var examBoardSchema = new mongoose.Schema({
  name: String,
  modules: [moduleSchema]
});


module.exports = mongoose.model("examBoard", examBoardSchema);

2 个答案:

答案 0 :(得分:1)

SELECT * FROM Companies c WHERE NOT EXISTS (SELECT 1 FROM Programs p WHERE p.CompanyId = c.Id AND p.Program = 'some title') 实现单例模式,在require模型时以mongoose形式实现新对象,如下所示:

require

答案 1 :(得分:1)

save方法仅适用于模型的实例,例如

var examboard = new examBoard();
examboard.property = 'something';
examboard.save();

实际上,当您说examBoard.insertMany();

时,您已经保存了对象