简体   繁体   中英

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.

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:

in my cmsview.js

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

my req.body.packageLid does not get populated. Any idea why? It just outputs "undefined"

You need to put axios call in componentDidMount method.

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 })
        })
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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