mongoose status:500内部服务器错误 Postman 在正文中输入数据时只会发送空的大括号

qpgpyjmq  于 2022-11-24  发布在  Go
关注(0)|答案(3)|浏览(113)

这是我的代码(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

qlvxas9a

qlvxas9a1#

您应该检查const user = await newUser.save();处的user对象中接收了什么
数据将以user.data为单位
或者改为使用json

const user = await newUser.save();
    res.status(200).jsonp(user);
piah890a

piah890a2#

.env文件必须位于根文件夹内,而不是根文件夹的外部

pcww981p

pcww981p3#

我在postman中遇到错误,问题是我在pgAdmin int字段中添加了一个字符串数据,

相关问题