0

Hello i'm trying to set the response i'am getting from my backend to State. But i allways get this error.

TypeError: this.setState is not a function

can someone pls tell me how i can set my response to the state ?

recalcRequest(data){
   let meth = data
   let brandCode = meth.brandCode
   let paymentAmount = this.props.location.query.anfrage.umsatz+"00"
   let merchantReference = this.props.location.query.cardid
   let jsonData = {
            brandCode : brandCode,
            paymentAmount : paymentAmount,
            merchantReference:merchantReference
                    };

  request.post({
    headers: {'content-type' : 'application/x-www-form-urlencoded'},
    url:     'http://localhost:8888/Backend/recalcSig.php',
    body:    "data=" + JSON.stringify(jsonData)
  }, function(err, response, body){
    if (err) throw err;
      body = JSON.parse(body)
      this.setState({
        merchantSig: body.merchantSig,
        sessionValidity: body.request.sessionValidity,
        requestPaymentMethods: false,
        showCardDetails:true
      })
    console.log('response: ', response);
  })

 }
HarryTgerman
  • 107
  • 1
  • 5
  • 19

1 Answers1

1

Use a arrow function for the callback.

request.post({
    headers: {'content-type' : 'application/x-www-form-urlencoded'},
    url:     'http://localhost:8888/Backend/recalcSig.php',
    body:    "data=" + JSON.stringify(jsonData)
  },(err, response, body) => {
    if (err) throw err;
      body = JSON.parse(body)
      this.setState({
        merchantSig: body.merchantSig,
        sessionValidity: body.request.sessionValidity,
        requestPaymentMethods: false,
        showCardDetails:true
      })
    console.log('response: ', response);
  })
PsykoSoldi3r
  • 409
  • 1
  • 4
  • 17