我正在尝试创建一个 api 服务器。我已经创建了我的用户模型并且正在尝试创建一个新用户,但是每次我都没有找到属性电子邮件(或任何其他通过的请求)。我不明白问题出在哪里。我正在测试在邮递员上运行它,但我遇到了同样的错误,当我尝试从 reactjs 发出发布请求时......它是一样的。如果我能得到答案,我会很高兴。非常感谢
这是我的服务器。
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const cors = require("cors");
// Configuring the database
const dbConfig = require("./config/database.config.js");
const mongoose = require("mongoose");
app.use(cors());
// Require Notes routes
require("./app/routes/user.routes.js")(app);
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse requests of content-type - application/json
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
next();
});
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose
.connect(dbConfig.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log("Successfully connected to the database");
})
.catch((err) => {
console.log("Could not connect to the database. Exiting now...", err);
process.exit();
});
// listen for requests
app.listen(5000, () => {
console.log("Server is listening on port 5000");
});
// define a simple route
app.get("/", (req, res) => {
res.json({
message: "Lolius Server is Running",
time: new Date().toString(),
});
});
这是路线
// Create and Save a new user
exports.create = (req, res) => {
User.find({ email: req.params.email })
.then((user) => {
if (!user === []) {
return res.status(500).send({
message: "username already exists, please try again.",
});
}
// Create a user
const userModel = new User({
email: req.body.email,
phone: req.body.phone,
password: req.body.password,
role: req.body.role,
verified: { status: false, code: makeId(4) },
location: {
lat: req.body.lat,
lng: req.body.lng,
},
});
// Save user in the database
userModel
.save()
.then((data) => {
res.send({ data, message: "User registered successfully" });
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the user.",
});
});
})
.catch((err) => {
return res.status(500).send({
message: err.message || "An error occurred try again",
});
});
};