Post 在节点 js express 中不工作
Post is not working in the node js express
当我尝试在应用程序中传递 post 请求时,它不起作用。当我尝试使用 postman 获取 post 数据时,数据没有传递,我得到以下值作为输出。
"_id": "615f2fe2fc52303bdb758e93", "saltSecret":
"a$AmJCbqiunWY2S8kVBbx.a.", "__v": 0
这些是我的代码。
app.js
require("./config/config");
require("./models/db");
const express = require("express");
const bodyParse = require("body-parser");
const cors = require("cors");
const rtsIndex = require("./route/index.router");
var app = express();
app.use(bodyParse.json());
app.use(cors());
app.use("/api", rtsIndex);
app.listen(process.env.PORT, () =>
console.log(`Server started at port : ${process.env.PORT}`)
);
studentModel.js
const mongoose = require("mongoose");
const bcrypt = require("bcryptjs");
var studentScheme = new mongoose.Schema({
fullname: {
type: String,
},
email: {
type: String,
},
password: {
type: String,
},
saltSecret: String,
});
studentScheme.pre("save", function (next) {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(this.password, salt, (err, hash) => {
this.password = hash;
this.saltSecret = salt;
next();
});
});
});
mongoose.model("Student", studentScheme);
studentController.js
const mongoose = require("mongoose");
const Student = mongoose.model("Student");
module.exports.resgiter = (req, res, next) => {
var student = new Student();
student.fullname = req.body.fullname;
student.email = req.body.email;
student.password = req.body.password;
student.save((err, doc) => {
if (!err) res.send(doc);
});
};
router.js
const express = require("express");
const router = express.Router();
const ctrlStudent = require("../controller/student.controller");
router.post("/register", ctrlStudent.resgiter);
module.exports = router;
请有人帮助我;我不明白我的代码有什么问题
大多数情况下,只要有一个承诺(不是 nodejs 中的确切承诺,只是我在这里使用的一个术语),它总是首选使用 async - await
returns。
所以通过使用 async - await
,我不知道你的数据是如何存储在你的数据库中的;但您可以尝试下面的代码并检查它是否有效:
const mongoose = require("mongoose");
const Student = mongoose.model("Student");
module.exports.resgiter = async (req, res, next) => {
var student = new Student({
fullname: req.body.fullname,
email: req.body.email,
password: req.body.password,
});
await student.save((err, doc) => {
if (!err) {
console.log("Saving the document...")
res.send(doc);
}
});
};
async - await
可以参考下面的链接
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
https://nodejs.dev/learn/modern-asynchronous-javascript-with-async-and-await
当我尝试在应用程序中传递 post 请求时,它不起作用。当我尝试使用 postman 获取 post 数据时,数据没有传递,我得到以下值作为输出。
"_id": "615f2fe2fc52303bdb758e93", "saltSecret": "a$AmJCbqiunWY2S8kVBbx.a.", "__v": 0
这些是我的代码。
app.js
require("./config/config");
require("./models/db");
const express = require("express");
const bodyParse = require("body-parser");
const cors = require("cors");
const rtsIndex = require("./route/index.router");
var app = express();
app.use(bodyParse.json());
app.use(cors());
app.use("/api", rtsIndex);
app.listen(process.env.PORT, () =>
console.log(`Server started at port : ${process.env.PORT}`)
);
studentModel.js
const mongoose = require("mongoose");
const bcrypt = require("bcryptjs");
var studentScheme = new mongoose.Schema({
fullname: {
type: String,
},
email: {
type: String,
},
password: {
type: String,
},
saltSecret: String,
});
studentScheme.pre("save", function (next) {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(this.password, salt, (err, hash) => {
this.password = hash;
this.saltSecret = salt;
next();
});
});
});
mongoose.model("Student", studentScheme);
studentController.js
const mongoose = require("mongoose");
const Student = mongoose.model("Student");
module.exports.resgiter = (req, res, next) => {
var student = new Student();
student.fullname = req.body.fullname;
student.email = req.body.email;
student.password = req.body.password;
student.save((err, doc) => {
if (!err) res.send(doc);
});
};
router.js
const express = require("express");
const router = express.Router();
const ctrlStudent = require("../controller/student.controller");
router.post("/register", ctrlStudent.resgiter);
module.exports = router;
请有人帮助我;我不明白我的代码有什么问题
大多数情况下,只要有一个承诺(不是 nodejs 中的确切承诺,只是我在这里使用的一个术语),它总是首选使用 async - await
returns。
所以通过使用 async - await
,我不知道你的数据是如何存储在你的数据库中的;但您可以尝试下面的代码并检查它是否有效:
const mongoose = require("mongoose");
const Student = mongoose.model("Student");
module.exports.resgiter = async (req, res, next) => {
var student = new Student({
fullname: req.body.fullname,
email: req.body.email,
password: req.body.password,
});
await student.save((err, doc) => {
if (!err) {
console.log("Saving the document...")
res.send(doc);
}
});
};
async - await
可以参考下面的链接https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
https://nodejs.dev/learn/modern-asynchronous-javascript-with-async-and-await