Unable to POST values by Postman Body form-data using Node API

Clash Royale CLAN TAG#URR8PPP
Unable to POST values by Postman Body form-data using Node API
I am using the following user controller to insert user using Node Express and MongoDB. When I started to insert values using Postman Body form-data, I am getting the following output as error_message "Invalid request, Invalid 'name' parameter." This error says that I want to insert value for name fiels in the Schema, but I entered the name field and its value in the Body form-data as Key and value, So why this did not work?
userController.js
const express=require("express");
const router =express.Router();
const mongoose=require("mongoose");
const multer = require('multer');
var assert = require('assert');
const User = require('../models/user.model.js');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var bodyParser = require('body-parser');
var app = express(); // define our app using express
// Put these statements before you define any routes.
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
// POST
exports.create = ("/",(req, res, next) =>
var varName= req.body.name;
var varUname = req.body.username;
var varPass = req.body.password;
var varEmail = req.body.email;
if(!varName)
return res.status(400).send(
"error_message": "Invalid request, Invalid 'name' parameter.",
"status": "INVALID_REQUEST"
);
// Other Validations are go here...
const user = new User(
name: varName, //required
address: req.body.address,
logo: req.body.logo,
username: varUname, //required
password: varPass, //required
phone_number: req.body.phone_number,
email: varEmail, //required
token_key : token
);
// Save Programe in the database
user.save()
.then(data =>
res.send(
"error_message": ,
"results":"User created successfully",
"status": "Success",
"token": token
);
).catch(err =>
res.status(500).send();
);
);
// GET all
exports.findAll = (req, res) =>
User.find()
.then(users =>
res.send(users);
).catch(err =>
res.status(500).send();
);
;
UPDATED, Please see my added server.js code
server.js
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
// Configuring the database
const dbConfig = require('./config/database.config.js');
var multer = require('multer');
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose.connect(dbConfig.url)
.then(() =>
console.log("Successfully connected to the database");
).catch(err =>
console.log('Could not connect to the database. Exiting now...');
process.exit();
);
app.use('/uploads', express.static('uploads'));
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.json());
app.use((req, res, next) =>
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS")
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json();
next();
);
// define a simple route
app.get('/', (req, res) =>
res.json("message": "Welcome to Agenda APP created by ApptimusTECH.");
);
require('./app/routes/programe.routes.js')(app);
require('./app/routes/user.routes.js')(app);
require('./app/routes/agenda.routes.js')(app);
// listen for requests
app.listen(3000, () =>
console.log("Server is listening on port 3000");
);
Why to use raw option and JSON? I am going to use Front end using react also.
– IamCoder
Aug 9 at 10:02
It's because your app is using JSON body-parsing middleware
app.use(bodyParser.json());– chridam
Aug 9 at 10:03
app.use(bodyParser.json());
If I use raw JSON, shall I insert data using react Front End?
– IamCoder
Aug 9 at 10:07
How to change
app.use(bodyParser.json()); to insert data using form-data ?– IamCoder
Aug 9 at 10:08
app.use(bodyParser.json());
form-data
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Change the raw type option in postman to JSON
– chridam
Aug 9 at 9:59