Categories
Database Development JSON MongoDB

How to Save data to mongodb database by JSON object?

I want to save data into the database. I used to check my URL by using database. But i couldn’t .
It shows this message :

DeprecationWarning: current URL string parser is deprecated, and will
be removed in a future version. To use the new parser, pass option {
useNewUrlParser: true } to MongoClient.connect. listning to port 3000
{ _id: 5d9775340007cc3c8cb34caf } { _id: 5d9775340007cc3c8cb34caf,
__v: 0 } Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

This is my router file code:

router.post("/timeTableRegistration", function(req, res) {
  const newTimeTable = new classTimeTable({
    object: req.body
  });
  console.log(newTimeTable);
  newTimeTable
    .save()
    .then(result => {
      console.log(result);
      res.json({ state: true, msg: "Data Inserted Successfully..!" });
    })
    .catch(error => {
      console.log(error);
      res.json({ state: false, msg: "Data Inserting Unsuccessfull..!" });
    });
  res.send("Success");
});

This is my model file:

const mongoose = require("mongoose");
const Schema = mongoose.schema;

const classTimeTableSchema = mongoose.Schema({
  className: { type: String, require: true },

  monday: {
    1: { type: String, require: true },
    2: { type: String, require: true }
  },
  tuesday: {
    1: { type: String, require: true },
    3: { type: String, require: true }
  }
});

const classTimeTable = (module.exports = mongoose.model(
  "classTimeTable",
  classTimeTableSchema
));

Leave an answer

Your email address will not be published. Required fields are marked *