0

I have a post interface that does not submit data correctly. test shows: req.body undefined

const express = require('express');
const router = express.Router();
const passport = require("passport");
const passportInfo = passport.authenticate('jwt',{ session: false });
const HomeSchema = require('../../../models/AnvizHome');
const homeBannerValidator = require('../../../validation/anviz/homeBanner');

router.post("/banner",passportInfo,(req,res) => {

const {msg,isValid} = homeBannerValidator(req.body);
if(!isValid){
    return res.status(400).json(msg);
}

HomeSchema.findOne({handle:req.body.handle}).then(banner => {
  console.log('current:   ' + req.body);
    const newBanner = {
        bannerBg:req.body.bannerBg,
        bannerName:req.body.bannerName,
        bannerSubName:req.body.bannerSubName,
        bannerFeather:req.body.bannerFeather,
        bannerLink:req.body.bannerLink
  };
  banner.prodcutBanner = newBanner;
  banner.then(home => res.json(home));
})
.catch((err) => res.json(err));
});

module.exports = router;

postman test: enter image description here

In fact, the terminal can see the returned data.

[Object: null prototype] {
   bannerBg: '5555555555555555555555555555555555',
   bannerName: 'THE GLOBAL LEADING',
   bannerSubName: 'PROVIDER OF INTELLIGENT SECURITY',
   bannerLink: 'www.anviz.com',
   handle: 'true' }

Seeking one or two!Thank you!

beso9595
  • 1,146
  • 3
  • 10
  • 16
  • Possible duplicate of [Express.js req.body undefined](https://stackoverflow.com/questions/9177049/express-js-req-body-undefined) – Stamos Jan 07 '19 at 09:51

2 Answers2

1

you forgot to import and use body parser https://www.npmjs.com/package/body-parser

var bodyParser = require('body-parser');
var express = require('express');
express.use(bodyParser.json());
express.use(bodyParser.urlencoded({ extended: false }));
Abdullah Khan
  • 649
  • 5
  • 11
0

I think I know the reason, because this is based on the user's token to submit data, so must be in the post according to the user id to determine whether the data is successful?

Here is the code that has been successfully saved, but I don't know right and wrong:

router.post("/banner",passportInfo,(req,res) => {

const {msg,isValid} = homeBannerValidator(req.body);
if(!isValid){
    return res.status(400).json(msg);
}

const profileFields = {};
profileFields.prodcutBanner = {};
if(req.body.handle) profileFields.handle = req.body.handle;
if(req.body.bannerBg) profileFields.prodcutBanner.bannerBg = req.body.bannerBg;
if(req.body.bannerName) profileFields.prodcutBanner.bannerName = req.body.bannerName;
if(req.body.bannerSubName) profileFields.prodcutBanner.bannerSubName = req.body.bannerSubName;
if(req.body.bannerFeather) profileFields.prodcutBanner.bannerFeather = req.body.bannerFeather;
if(req.body.bannerLink) profileFields.prodcutBanner.bannerLink = req.body.bannerLink;


HomeSchema.findOne({user: req.user.id}).then(profile => {
  if(profile){
    HomeSchema.findByIdAndUpdate({user: req.user.id},{$set:profileFields},{new:true}).then(profile => res.json(profile));
  }else{
    HomeSchema.findOne({handle:profileFields.handle}).then(profile => {
        if(profile){
            msg.handle = "The user's handle personal information already exists, please do not re-create it!";
            res.status(400).json(msg);
        }
        new HomeSchema(profileFields).save().then(profile => res.json(profile));
    })
  }
})
.catch((err) => res.json(err));});


module.exports = router;