Javascript 无法发布/api/父项
我是Nodejs的新手,正在用它做一个项目。现在,我在postman上收到一个错误404(无法发布/api/parents)。 我真的不知道我在节点上错过了一两件事。求你了,我需要帮助。我的代码如下: 模型文件夹=>Parent.js:Javascript 无法发布/api/父项,javascript,node.js,Javascript,Node.js,我是Nodejs的新手,正在用它做一个项目。现在,我在postman上收到一个错误404(无法发布/api/parents)。 我真的不知道我在节点上错过了一两件事。求你了,我需要帮助。我的代码如下: 模型文件夹=>Parent.js: const mongoose = require("mongoose"); const ParentSchema = mongoose.Schema({ user: { type: mongoose.Schema.Types.O
const mongoose = require("mongoose");
const ParentSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "users",
},
motherName: {
type: String,
required: true,
},
fatherName: {
type: String,
required: true,
},
phoneNumb: {
type: String,
required: true,
},
emailAdd: {
type: String,
required: true,
},
nin: {
type: String,
required: true,
},
parentResAddress: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("parent", ParentSchema);
路由文件夹=>parents.js:
const express = require("express");
const router = express.Router();
const auth = require("../middleware/auth");
const { check, validationResult } = require("express-validator");
const User = require("../models/User");
const Parent = require("../models/Parent");
// @route POST api/parents
// @desc Add new parents
// @access Private
router.post(
"/",
auth,
[check("emailAdd", "Email Address is required").not().isEmpty()],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const {
motherName,
fatherName,
phoneNumb,
emailAdd,
nin,
parentResAddress,
} = req.body;
try {
const newParent = new Parent({
motherName,
fatherName,
phoneNumb,
emailAdd,
nin,
parentResAddress,
user: req.user.id,
});
const parent = await newParent.save();
res.json(parent);
} catch (err) {
console.error(err.message);
res.status(500).send("Server Error");
}
}
);
module.exports = router;
需要通过更改在路由器中注册路径
router.post(
"/",
auth,
到
您的代码
路由器中没有/api/parents
路由。post(“/”,某物)
这只适用于/不适用于/api/parentsThanks@aRvi。事实上,我错过了server.js上api的路由。实际上,我已经将父路由添加到server.js,它开始工作了。再次感谢
router.post(
"/api/parents",
auth,