简体   繁体   English

如何获取请求正文

[英]How to get request body to be populated

I am doing a get response and in my api/cmsview it is not getting the object that my Axios is passing. 我正在做一个get响应,在我的api / cmsview中,它没有得到我的Axios传递的对象。

class CmsView extends Component {
  constructor(props) {
    super(props)


    this.state = {
        cmsObj: [],
        packageLid : props.location.state
    }
    var packageLid = this.props.location.state.packageLid
    console.log(packageLid.PACKAGE_LID) //this gets populated with data
    Axios.get('/api/cmsview', {packageLid})
    .then((response) => {
                this.setState({ cmsObj: response.data })
            })
   }
}

My packageLid does get populated with data, but when I do the Axios get: 我的packageLid确实填充了数据,但当我执行Axios时:

in my cmsview.js 在我的cmsview.js中

router.get('/', (req, res, next) => {
    console.log(req.body.packageLid.PACKAGE_LID) 
}

my req.body.packageLid does not get populated. 我的req.body.packageLid没有填充。 Any idea why? 知道为什么吗? It just outputs "undefined" 它只输出“未定义”

You need to put axios call in componentDidMount method. 您需要在componentDidMount方法中调用axios。

componentDidMount() {
var packageLid = this.props.location.state.packageLid
console.log(packageLid.PACKAGE_LID) //this gets populated with data
Axios.get('/api/cmsview', {packageLid})
.then((response) => {
            this.setState({ cmsObj: response.data })
        })
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM