类型错误Employee不是构造函数node.js

时间:2017-08-26 22:21:28

标签: javascript node.js mongodb mongoose

每当我尝试post方法到localhost:3001 / employees时我得到一个类型错误Employee不是构造函数。我已经在employee.js文件中定义了架构,并在app.js中将其链接起来,我有点不知道为什么它没有运行。任何帮助将不胜感激!!

Employee.js

<!-- language: lang-js -->

    var mongoose = require("mongoose");

    var EmployeeSchema = new mongoose.Schema({
      _id: {
        type: Number,
        required: true
      },
      first_name: {
        type: String,
        required: true
      },
      last_name: {
        type: String,
        required: true
      },
      title: {
        type: String,
        required: true
      },
      start_date: {
        type: String,
        required: true
      },
      birth_date: {
        type: Number,
        required: true
      },
      wage: {
        type: String,
        required: true
      }
    })

    var Employee = mongoose.model("Employee", EmployeeSchema);

    module.exports = Employee;


<!-- end snippet -->

employees.js

<!-- language: lang-js -->

    var express = require("express");
    var mongodb = require("mongodb");
    var app = express();
    var router = express.Router();
    var mongoose = require("mongoose");
    var Employee = mongoose.model("Employee");

    router.post("/", (req,res) => {
      var Employee = new Employee({
        _id: req.body._id,
        first_name: req.body.first_name,
        last_name: req.body.last_name,
        title: req.body.title,
        start_date: req.body.start_date,
        birth_date: req.body.birth_date,
        wage: req.body.wage
      })

      Employee.save((err, result) => {
        if(err) {
          res.send(err);
        } else {
          res.send(result);
        }
      })
    })

    router.get("/", (req, res) => {
      Employee.find(function (err, employees) {
        if (err) {
          res.send(err);
        } else {
          res.send(employees);
        }
      })
    })

    router.get("/:id", (req, res) => {
      var employeeid = new mongodb.ObjectID(req.params["id"]);
      Employee.find({"_id": employeeid},function (err, employees) {
        if (err) {
          res.send(err);
        } else {
          res.send(employees);
        }
      })
    })


    router.put("/", (req, res) => {
      var employeeid = new mongodb.ObjectID(req.params["id"]);
      Employee.find({"_id": employeeid},function (err, employees) {
        if (err) {
            res.status(500).send(err);
        } else {
            employee.title = req.body.title || employee.title;
            employee.description = req.body.description || employee.description;
            employee.price = req.body.price || employee.price;
            employee.completed = req.body.completed || employee.completed;

            Employee.save(function (err, employee) {
                if (err) {
                    res.status(500).send(err)
                }
                res.send(employee);
            });
        }
    });
    })

    router.delete("/:id", (req, res) => {
      var employeeid = new mongodb.ObjectID(req.params["id"]);
      Employee.remove({_id: employeeid}).then(() => {
        res.send("success");
      })
    })

    module.exports = router;


<!-- end snippet -->

app.js

<!-- language: lang-js -->

    var express = require("express");
    var bodyParser = require("body-parser");
    var lessMiddleware = require('less-middleware');
    var mongoose = require('mongoose');
    var port = 3001;
    var app = express();
    var router = express.Router();
    var path = __dirname + '/views/';
    require("./Employee");
    require("./user");
    var employees = require("./employees.js");
    var users = require("./users.js");

    mongoose.connect("mongodb://kenth56:123@ds034807.mlab.com:34807/database_baby", {
      useMongoClient: true
    }).then(() => {
      console.log("db connected");
    }, ((err) => {
        console.log(err);
    }))

    app.use(bodyParser.json());
    app.set("view engine", "ejs");
    app.use(lessMiddleware(path));
    app.use(express.static(path));
    app.use("/",router);

    router.get("/", (req,res) => {
      res.sendFile(path + "index.html");
    })

    app.use("/employees", employees);

    app.use("/users", users);

    app.listen(port, () => {
      console.log("Live at Port " + port);
    })

    router.use( (req,res,next) => {
      console.log("/" + req.method);
      next();
    })

    app.use("*", (req,res) => {
      res.sendFile(path + "404.html");
    })


<!-- end snippet -->

1 个答案:

答案 0 :(得分:1)

原因很可能是因为您声明了var Employee并且覆盖了您对Employee的其他声明。尝试var e = new Employee()。​​