gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:10:14 25 4
gpt4 key购买 nike

每当我尝试对 localhost:3001/employees 执行 post 方法时,我都会收到类型错误 Employee is not a constructor。我已经在employee.js 文件中定义了架构,并将其链接到app.js 中,我有点不明白为什么它没有运行。任何帮助将不胜感激!!

员工.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 -->

最佳答案

原因很可能是因为您声明了 var Employee 并且覆盖了您为 Employee 所做的其他声明。尝试使用 var e = new Employee() 代替。

关于javascript - 类型错误 Employee 不是构造函数 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com