我是新来的IT,我开始通过视频课程学习React和JS。在为视频课程创建网站时,我遇到了许多错误:当我试图发送一个用try catch封装的请求时,我总是得到一个错误。我不明白为什么会这样,请帮忙。
index.js
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const authRoute = require("./routes/auth");
dotenv.config();
mongoose
.connect(process.env.URL_MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Successfully connected"))
.catch((err) => {
console.error(err);
});
app.use(express.json());
app.use("/api/auth", authRoute);
app.listen(8800, () => {
console.log("Server is running!");
});
User.js
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema(
{
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
profilePic: { type: String, defaut: "" },
isAdmin: { type: Boolean, default: false },
},
{ timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
auth.js
const router = require("express").Router();
const User = require("../models/User");
router.post("/registration", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
})
try {
const user = await newUser.save();
res.status(201).json(user);
} catch (err) {
res.status(500).json(err);
}
});
module.exports = router;
注册请求
第二部分邮差
我在你的img上看到,你和我一样在克隆Netflix。
我可以看到你对邮差的要求是:
{
"username" = "eeqeqeqw",
"email": "nova@gmail.com",
"password":"2221"
}
所以当处理对象时,值被赋值为":"而不是"="象征。
尝试这样做相同的请求:
{
"username": "eeqeqeqw",
"email": "nova@gmail.com",
"password":"2221"
}