如何创建允许我 POST 的构造函数类型?
How do I create a constructor type that lets me POST?
所以我一直在尝试为我的滑翔伞社区创建一个后端项目,我正在与邮递员一起测试功能,而我的朋友正在处理前端。
我创建了很多模型,在与其他人打交道之前,我试图检查用户模型,但我做不到。它会说 TypeError: User is not a constructor 当我 POST 通过 postman.
routes.js
const express = require("express");
const {userModel} = require("../schemas/db-schema");
const {EventModel} = require("../schemas/db-schema");
const {PaymentModel} = require("../schemas/db-schema");
const {RulesModel} = require("../schemas/db-schema");
const {InvitationModel} = require("../schemas/db-schema");
const app = express();
app.post("/add_user", async (request, response) => {
const user = new userModel(request.body);
try {
await user.save();
response.send(user);
} catch (error) {
response.status(500).send(error);
}
});
app.get("/users", async (request, response) => {
const users = await userModel.find({});
try {
response.send(users);
} catch (error) {
response.status(500).send(error);
}
});
module.exports = app;
db-schema.js
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
Name: {type:String, required: true},
Surname:{type:String, required: true},
Email: {type:String, required: true},
Birth: {type:Date,required: false},
AbsenceCount: {type:Number, required:true},
Height: {type:Number, required:true},
Weight: {type:Number, required:true},
FlightCount:{type:Number, required:true},
BloodType: {type:String, required: true},
Sex: {type:String, required: true},
PhoneNum: {type:String, required: true},
Siv: {type:Boolean, required: true},
Type: {type:String, required: true},
Password: {type:String, required: true},
Illness: [{type:String, required: true}],
Drugs: [{type:String, required: true}],
Adresses: [{type:String, required: true}],
EmergencyName: [{type:String, required: true}],
emergencyPhoneNum: [{type:String, required: true}]
}
);
const UserModel = mongoose.model("UserModel", UserSchema);
const eventSchema = new mongoose.Schema({
Name: {type:String, required: true},
Location: {type:String, required: true},
Date: {type:Date, required: true},
Deadline: {type:Date, required: true},
MaximumParticipant: {type:Number, required: true},
Participants:[{type:String, required: true}]
});
const EventModel = mongoose.model("EventModel", eventSchema);
const paymentSchema = new mongoose.Schema({
Name: {type:String, required: true},
DeadLine: {type:Date, required: true},
PaymentCategory:{type:String, required: true},
PaymentAmount: {type:Number, required: true},
PeopleInDebt:[{type:String, required: true}]
});
const PaymentModel = mongoose.model("PaymentModel", paymentSchema);
const ruleSchema = new mongoose.Schema({
EventStartDate: {type:Date, required: true},
EventFinishDate:{type:Date, required: true},
EventStartHour:{type:String, required: true},
EventFinishHour: {type:String, required: true},
BeginnerPilotEventRegister: {type:Boolean, required: true},
ActivePilotEventRegister: {type:Boolean, required: true},
CanRegisterEvents: {type:Boolean, required: true},
NewMemberRegister: {type:Boolean, required: true}
});
const RulesModel = mongoose.model("RulesModel", ruleSchema);
const invitationSchema = new mongoose.Schema({
Name:{type:String, required: true},
SpecialCode: {type:String, required: true},
Deadline:{type:Date, required: true},
Type: {type:String, required: true},
UsageAmount:{type:Number, required: true}
});
const InvitationModel = mongoose.model("InvitationModel", invitationSchema);
module.exports = {
InvitationModel,RulesModel,PaymentModel,EventModel,UserModel
}
您正在导出 UserModel
并尝试获取 userModel
。在路由中将 u
更改为大写或在 db-schema 中更改为小写,它将起作用
所以我一直在尝试为我的滑翔伞社区创建一个后端项目,我正在与邮递员一起测试功能,而我的朋友正在处理前端。
我创建了很多模型,在与其他人打交道之前,我试图检查用户模型,但我做不到。它会说 TypeError: User is not a constructor 当我 POST 通过 postman.
routes.js
const express = require("express");
const {userModel} = require("../schemas/db-schema");
const {EventModel} = require("../schemas/db-schema");
const {PaymentModel} = require("../schemas/db-schema");
const {RulesModel} = require("../schemas/db-schema");
const {InvitationModel} = require("../schemas/db-schema");
const app = express();
app.post("/add_user", async (request, response) => {
const user = new userModel(request.body);
try {
await user.save();
response.send(user);
} catch (error) {
response.status(500).send(error);
}
});
app.get("/users", async (request, response) => {
const users = await userModel.find({});
try {
response.send(users);
} catch (error) {
response.status(500).send(error);
}
});
module.exports = app;
db-schema.js
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
Name: {type:String, required: true},
Surname:{type:String, required: true},
Email: {type:String, required: true},
Birth: {type:Date,required: false},
AbsenceCount: {type:Number, required:true},
Height: {type:Number, required:true},
Weight: {type:Number, required:true},
FlightCount:{type:Number, required:true},
BloodType: {type:String, required: true},
Sex: {type:String, required: true},
PhoneNum: {type:String, required: true},
Siv: {type:Boolean, required: true},
Type: {type:String, required: true},
Password: {type:String, required: true},
Illness: [{type:String, required: true}],
Drugs: [{type:String, required: true}],
Adresses: [{type:String, required: true}],
EmergencyName: [{type:String, required: true}],
emergencyPhoneNum: [{type:String, required: true}]
}
);
const UserModel = mongoose.model("UserModel", UserSchema);
const eventSchema = new mongoose.Schema({
Name: {type:String, required: true},
Location: {type:String, required: true},
Date: {type:Date, required: true},
Deadline: {type:Date, required: true},
MaximumParticipant: {type:Number, required: true},
Participants:[{type:String, required: true}]
});
const EventModel = mongoose.model("EventModel", eventSchema);
const paymentSchema = new mongoose.Schema({
Name: {type:String, required: true},
DeadLine: {type:Date, required: true},
PaymentCategory:{type:String, required: true},
PaymentAmount: {type:Number, required: true},
PeopleInDebt:[{type:String, required: true}]
});
const PaymentModel = mongoose.model("PaymentModel", paymentSchema);
const ruleSchema = new mongoose.Schema({
EventStartDate: {type:Date, required: true},
EventFinishDate:{type:Date, required: true},
EventStartHour:{type:String, required: true},
EventFinishHour: {type:String, required: true},
BeginnerPilotEventRegister: {type:Boolean, required: true},
ActivePilotEventRegister: {type:Boolean, required: true},
CanRegisterEvents: {type:Boolean, required: true},
NewMemberRegister: {type:Boolean, required: true}
});
const RulesModel = mongoose.model("RulesModel", ruleSchema);
const invitationSchema = new mongoose.Schema({
Name:{type:String, required: true},
SpecialCode: {type:String, required: true},
Deadline:{type:Date, required: true},
Type: {type:String, required: true},
UsageAmount:{type:Number, required: true}
});
const InvitationModel = mongoose.model("InvitationModel", invitationSchema);
module.exports = {
InvitationModel,RulesModel,PaymentModel,EventModel,UserModel
}
您正在导出 UserModel
并尝试获取 userModel
。在路由中将 u
更改为大写或在 db-schema 中更改为小写,它将起作用