快速路由器,req.body未定义

时间:2019-01-07 09:44:38

标签: node.js rest express router

我的发布界面无法正确提交数据。 测试表明:req.body未定义

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;

邮递员测试: enter image description here

实际上,终端可以看到返回的数据。

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

寻求一两个!谢谢!

2 个答案:

答案 0 :(得分:1)

您忘记导入和使用正文解析器 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 }));

答案 1 :(得分:0)

我想我知道原因,因为这是基于用户的令牌提交数据的,所以必须在帖子中根据用户ID来确定数据是否成功?

以下是已成功保存的代码,但我不知道对与错:

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;