Search code examples
mongodbexpressmongoosemongoose-schema

Mongoose data is not saved to db


I'm trying to use mongoose/express to add form data to a db. The collection is created within the database and although there are no errors when submitting, and my console.log confirms the data is there along with a generated ID i still don't have any entries within the database, can you see what I'm missing? Any help would be really appreciated.


    **CONFIG**
      const port = process.env.PORT || 3000,
            bodyparser = require("body-parser"),
      mongoose = require("mongoose"),
      express = require("express"),
      app = express();

    app.set("view engine", "ejs");

    app.use(
      bodyparser.urlencoded({
        extended: true
      })
    );
    app.use(
      require("express-session")({
        secret: "Mama Mia Thats a Spicy Meatball!",
        resave: false,
        saveUninitialized: false
      })
    );

    app.use(express.static(__dirname + "/styles/"));
    app.use(express.static(__dirname + "/Media/"));
    app.use(express.static(__dirname + "/JS/"));

    mongoose.connect("mongodb://localhost/lashB", {
      useNewUrlParser: true
    });

    const mongCon = mongoose.connection;

    mongCon.on("connected", function () {
      console.log(`beep boop 
        database mainframe syncroniiiiiiized`);
    });

    mongCon.on("disconnected", function () {
      console.log(`You're off the database now...Daniel San.`);
    });

    mongCon.on('error', function () {
      console.log(error, `We've got company...there's an error!!!`);
    });

    **SCHEMA**
    var customerSchema = new mongoose.Schema({
      firstName: String,
      lastName: String,
      mobile: String,
      email: String,
      treatment: String,
      time: String,
      date: Date
    });

    var Customer = mongoose.model("Customer", customerSchema, "Customer");


    **ROUTES**
    app.get("/", function (req, res) {
      res.render("main");
    });

    app.get("/appointment", function (req, res) {
      res.render("appointment");
    });

    app.get("/appointmentConfirmation", function (req, res) {
      res.render("appConfirmation");
    });

    app.get("/blogHome", function (req, res) {
      res.render("blogHome");
    });

    app.post('/appointment', function (req, res) {
      var firstName = req.body.firstName,
        lastName = req.body.lastName,
        mobile = req.body.mobile,
        email = req.body.email,
        treatment = req.body.treatment,
        time = req.body.time,
        date = req.body.date;
      var deetz = {
        date: date,
        time: time,
        treatment: treatment,
        email: email,
        mobile: mobile,
        firstName: firstName,
        lastName: lastName
      }

      Customer.create(deetz, function (err, info) {
        if (err) {
          console.log(err, `something went wrong`);
        } else {
          console.log(info);
          console.log('newly created file for a customer');
        }
      });

      res.render('appConfirmation', {
          firstName: firstName
        }

      );
    });

    app.listen(port, function () {
      console.log(`You rockin' now on port ${port}`);
    });


Solution

  • Fixed it. The mistake was that i was entering 'use' nameofcollection, but 'use' is for the db and not collections. So show dbs > use nameofdb > db.nameofcollection.find() worked just fine.