title: Awaiting Seed Data Before Accepting Server Requests
body: <p>As the titles suggest the issue here is a function in the server.js running after the base root has been loaded. Below you can see the function call and the root.</p>
  
  <pre><code>seedDB();
  app.get("/",function(req,res)
  {
      examBoard.find({}, function (err, examBoard)
      {
          console.log("examBoard.length: " + examBoard.length);
          res.render("landing", { examBoard: examBoard });
      });
  });
  </code></pre>
  
  <p>The function does basic seeding of the database and thus must run before the base root. It outputs what you can see in the following image (most of the output is cut off).</p>
  
  <p><a href="https://i.stack.imgur.com/8Otog.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Otog.png" alt="enter image description here"></a></p>
  
  <p>The output in the red box is the output as result of the <code>console.log</code> in the base root. Here is the <code>app.listen</code> which is at the very bottom of the code, with everything above it.</p>
  
  <pre><code>app.listen(process.env.PORT, process.env.IP,function()
  {
      console.log("Server started");
  });
  </code></pre>
  
  <p>Here is the code for <code>seedDB</code> with full code including the arrays in this hastebin link (<a href="https://hastebin.com/acecofoqap.lua" rel="nofollow noreferrer">https://hastebin.com/acecofoqap.lua</a>) (thought it would be a bit excessive to include them as they are rather large):</p>
  
  <pre><code>function seedDB() {
      user.remove({}, function (err) {
          if (err) {
              console.log("Could not remove user\n" + err);
          }
          else {
              console.log("Removed old user");
              examBoard.remove({}, function (err) {
                  if (err) {
                      console.log("Could not remove examboards\n" + err);
                  }
                  else {
                      console.log("Removed old examboards");
                      question.remove({}, function (err) {
                          if (err) {
                              console.log("Could not remove questions\n" + err);
                          }
                          else {
                              console.log("Removed old questions");
                              user.register(new user
                                  ({
                                      username: "admin",
                                      email: "jonathanwoollettlight@gmail.com",
                                      role: "admin"
                                  }),
                                  "lu134r7n75q5psbzwgch", function (err, user) {
                                      if (err) {
                                          console.log("Failed to add admin\n" + err);
                                      }
                                      else {
                                          console.log("Admin added");
                                          examboardData.forEach(function (examSeed) {
                                              examBoard.create(examSeed, function (err, exam) {
                                                  console.log("Creating new examboard");
                                                  if (err) {
                                                      console.log("Could not create new examboard\n" + err);
                                                  }
                                                  else {
                                                      console.log("Created examboard");
                                                  }
                                              });
                                          });
                                          var topicIncrementor = 0;
                                          questionData.forEach(function (questionSeed) {
                                              question.create(questionSeed, function (err, question) {
                                                  if (err) {
                                                      console.log("Could not create new question\n" + err);
                                                  }
                                                  else {
                                                      console.log("Created question");
                                                      examBoard.find({}, function (err, exams) {
                                                          for (var i = 0; i &lt; exams.length; i++) {
                                                              for (var t = 0; t &lt; exams[i].modules.length; t++) {
                                                                  for (var q = math.floor(topicIncrementor / 12); q &lt; exams[i].modules[t].topics.length; q++) {
                                                                      exams[i].modules[t].topics[q].questions.push(question);
                                                                      topicIncrementor++;
                                                                  }
                                                                  topicIncrementor = 0;
                                                              }
                                                              exams[i].save();
                                                          }
                                                      });
                                                  }
                                              });
                                          });
                                      }
                                  });
                          }
                      });
                  }
              });
          }
      });
  }
  module.exports = seedDB;
  </code></pre>
  
  <p>For my program to work here the <code>seedDB</code> function must run before the base root, if you can provide a solution or merely point me in the right direction it would be greatly appreciated.</p>