khalid dahir
khalid dahir

Reputation: 63

status : 500 Internal Server Error postman only posts empty curly brackets when enter the data on the body

and this is my code (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 
when I send the request postman posts empty curly brackets. I want to get the post data on the down of the body at the postman

screenshot of the postman

and this is what terminal logs result on the terminal

Upvotes: 1

Views: 1671

Answers (3)

Lesly
Lesly

Reputation: 1

I got that error in postman and my problem was that I was adding a str data in an int field in pgAdmin

Upvotes: 0

khalid dahir
khalid dahir

Reputation: 63

.env file must be inside the root folder, not the outside

Upvotes: 1

Abhishek Singh
Abhishek Singh

Reputation: 527

You should check what receives in user object at const user = await newUser.save();

data would be in user.data

or use json instead,

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

Upvotes: 1

Related Questions