Person isn't a constructor

Viewed 32

I am trying to setup a simple CRUD REST Api using Node.js, Express and MongoDB. I managed to get it up and running without errors (node server.js works fine, no exceptions).

However, when i try to insert something in the database (using Postman), i do receive an error saying "Person isn't a constructor", pointing exactly to my person controller and the line where i create a new person (supposing i'm inserting it). I've tried a few things to debug it here but no success. Any input is very appreciated :)

I'll show here the files related to it and my request on Postman. Just for the record: when debugging it i can see that the data is well received (nothing strange happening).

person-controller.js

const db = require("../models");
const Person = db.persons;

// Create and Save a new Person
exports.create = (req, res) => {
   // Validate request
   if (!req.body.name) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  // Create a Person
  const person = new Person({
    name: req.body.name,
    surname: req.body.surname,
    phone: req.body.phone,
    address: req.body.address,
    active: req.body.active ? req.body.active : false
  });
  
  // Save Person in the database
  person
    .save(person)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Person."
      });
    });
};

// Retrieve all Persons from the database.
exports.findAll = (req, res) => {
    const name = req.query.name;
    var condition = name ? { title: { $regex: new RegExp(name), $options: "i" } } : {};
  
    Person.find(condition)
      .then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving persons"
        });
      });
};

// Find a single Person with an id
exports.findOne = (req, res) => {
    const id = req.params.id;

    Person.findById(id)
      .then(data => {
        if (!data)
          res.status(404).send({ message: "No person found with this id " + id });
        else res.send(data);
      })
      .catch(err => {
        res
          .status(500)
          .send({ message: "Error retrieving Person with id=" + id });
      });
};

// Update a Person by the id in the request
exports.update = (req, res) => {
    if (!req.body) {
        return res.status(400).send({
          message: "Data to update can not be empty!"
        });
      }
    
      const id = req.params.id;
    
      Person.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
        .then(data => {
          if (!data) {
            res.status(404).send({
              message: `Cannot update Person with id=${id}. Maybe Person was not found!`
            });
          } else res.send({ message: "Person was updated successfully." });
        })
        .catch(err => {
          res.status(500).send({
            message: "Error updating Person with id=" + id
          });
        });
};

// Delete a Person with the specified id in the request
exports.delete = (req, res) => {
    const id = req.params.id;

    Person.findByIdAndRemove(id)
      .then(data => {
        if (!data) {
          res.status(404).send({
            message: `Cannot delete Person with id=${id}. Maybe Person was not found!`
          });
        } else {
          res.send({
            message: "Person was deleted successfully!"
          });
        }
      })
      .catch(err => {
        res.status(500).send({
          message: "Could not delete Person with id=" + id
        });
      });
};

// Delete all Persons from the database.
exports.deleteAll = (req, res) => {
    Person.deleteMany({})
    .then(data => {
      res.send({
        message: `${data.deletedCount} Persons were deleted successfully!`
      });
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while removing all persons."
      });
    });
};

// Find all active persons
exports.findAllActive = (req, res) => {
    Person.find({ active: true })
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving persons."
      });
    });
};

person.model.js

module.exports = mongoose => {
  var schema = mongoose.Schema(
    {
      name: String,
      surname: String,
      phone: String,
      address: String,
      active: Boolean
    },
    {timestamps: true}
  );

  schema.method("toJSON",function() {
    const {__v, id, ...object } = this.toObject();
    object.id = _id;
    return object;
  });

  const Person = mongoose.model("person",schema)
  return Person;
};
/*


module.exports = mongoose => {
    const Person = mongoose.model(
      "person",
      mongoose.Schema(
        {
          name: String,
          surname: String,
          phone: String,
          address: String,
          active: Boolean
        },
        { timestamps: true }
      )
    );
  
    return Person;
  };*/

Postman screen: https://imgur.com/a/9gAz2Pq

0 Answers
Related