这是我的代码(index.js),(auth.js)
const express = require("express");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const authRoute = require("./routes/auth");
const app = express();
dotenv.config();
app.use(express.json());
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}).then(console.log("Connected to MongoDB"))
.catch((err) => console.log("err"));
app.use("/api/auth",authRoute );
app.use("/api/auth",authRoute);
app.listen("3000", () => {
console.log("Backend is running.");
});
const router = require("express").Router();
const User = require("../models/User");
//REGISTER
router.post("/register", async (req, res) => {
try {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
});
const user = await newUser.save();
res.status(200).json(user);
} catch (err) {
res.status(500).json(err);
}
});
//LOGIN
module.exports = router;
and image of the postman result
当我发送请求时, Postman 的帖子空花括号。我想在 Postman 的身体下面得到帖子数据
screenshot of the postman
这是终端记录的内容result on the terminal
3条答案
按热度按时间qlvxas9a1#
您应该检查
const user = await newUser.save();
处的user
对象中接收了什么数据将以
user.data
为单位或者改为使用
json
,piah890a2#
.env文件必须位于根文件夹内,而不是根文件夹的外部
pcww981p3#
我在postman中遇到错误,问题是我在pgAdmin int字段中添加了一个字符串数据,