0

I believe the issue revolves around the use of .save() and could possibly be associated with the object not having a .save() method.

/ This is the debugger output (thought I might as well clarify) /

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

Just to help anyone find the actual issue, I have used examBoad.save(); twice (once after the creation of all the examBoards and once close to the end of the program) and question.save(); once, I believe the problem to be with 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;
The examBoard model
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
  • can you provide the ./models/examBoard file Commented Sep 28, 2016 at 19:44
  • Just added it, sorry for not thinking of doing that. Commented Sep 28, 2016 at 20:38

2 Answers 2

1

require implements a singleton pattern, in mongoose when require a model, it's necessary to instance a new object, something like this:

var exam = new examBoard(request.body); // request.body can to contain the values of the schema structure
exam.save();
Sign up to request clarification or add additional context in comments.

2 Comments

Not wholly sure what you mean do you mean I should do: var exam=new examBoard("./models/examBoard"); ?
@JonathanWoollett-light It means that multiple require return single instance of module in node.js. Your examBoard file exports single value, which i guess is a function - constructor of model - and not a model instance itself. Thus it doesn't have save() method - it just not supposed to.
1

The save method are only on the instance of an model, example

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

actually you already saving the objects when you say examBoard.insertMany();

3 Comments

Do you mean to do: var mongoose = require("mongoose"); var examBoard = require("./models/examBoard"); var question = require("./models/question"); var user = require("./models/user"); var examboard = new examBoard(); function seedDB(){ Then replace all 'examBoard' within the function seedDB to 'examboard'
not exactly i am explaining that the save method is only for one object, if you want to insert many object need to be call from the collection itself as you are doing, see the mongoose doc mongoosejs.com/docs/models.html for a better explanation, in the section Constructing documents
how exactly would I fix this problem? I am very thankful for you telling me what the problem is, but (I think largely down to my ineptitude) I don't know how to solve it, and googling it has been of little help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.