0

I'm trying to build server side back-end code for my website. I tried app.get request in postman and it worked but when I tried app.post request in postman it didn't work and gave me errors.

I tried all the solution that was available online and I could understand (I'm Ubuntu user).

Error Screenshot that I get in Postman

The following image will show you the error and format I used in postman

Server.js File (main server file)

const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");

const app = express();
const mongoose = require("mongoose");
require("dotenv").config();

mongoose.Promise = global.Promise;
mongoose
  .connect(process.env.DATABASE, { useNewUrlParser: true })
  .then(() => console.log("MongoDB Connected"))
  .catch(err => console.log(err));

// // DB config
mongoose.set("useCreateIndex", true);

// const db = require("./config/keys").mongoURI;

// Connect to MongoDB

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());

// Models
const { User } = require("./models/user");

//====================================================
//                      USERS
//====================================================

app.post("/api/users/register", (req, res) => {
  const user = new User(req.body);

  user.save((err, doc) => {
    if (err) return res.json({ success: false, err });
    res.status(200).json({ success: true, userdata: doc });
  });
});

app.get("/", (req, res) => res.send("hello world"));

const port = process.env.PORT || 3002;

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

User Model file (models/user.js)

const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
  email: {
    type: String,
    requrired: true,
    trim: true,
    unique: 1
  },
  password: {
    type: String,
    requrired: true,
    minlength: 5
  },
  name: {
    type: String,
    requrired: true,
    maxlength: 100
  },
  lastname: {
    type: String,
    requrired: true,
    maxlength: 100
  },
  cart: {
    type: Array,
    default: []
  },
  history: {
    type: Array,
    default: []
  },
  role: {
    type: Number,
    default: 0
  },
  token: {
    type: String
  }
});

const User = mongoose.model("User", userSchema);

module.exports = { User };
rohanisme
  • 23
  • 1
  • 5

3 Answers3

0

Postman request should be like below.

{
   "email": "rohit***@gmail.com",
   "password": "password@123",
   "name": "sher",
   "lastname": "lock"
}
narayansharma91
  • 2,273
  • 1
  • 12
  • 20
0

You are sending an invalid JSON.

Use this JSON for sending Request.

 {
      "email":"rohan3131313@gmail.com",
      "password":"password@123",
      "name":"sher",
      "lastname:"lock"
 }
Jitendra virani
  • 366
  • 4
  • 8
0

pass this has a raw data from postman and then call the post api

{
   "email": "rohan@getMaxListeners.com",
   "password":"pass@123",
   "name":"sher",
   "lastname":"lock"
}
Narendra Chouhan
  • 2,291
  • 1
  • 15
  • 24