Search code examples
reactjscallbackrequestsuperagent

Call function in callback of Superagent - React


I want to call a function at the end of request (when I get the response) with Superagent.

request.post('/TextUpload')
.send({title:this.state.title1})
.end( function(res){
   console.log(res);
   this.myFunction();
})

But I get the error : this is null or undefined.

MyFunction() is declared and binded in the constructor. I cannot write the code of the function directly in the callback because I do this.props.refresh(true); (it sends Data to the parent)


Solution

  • I get the error : this is null or undefined.?

    This should work.Use arrow function to get the lexical scope binding

    .end((res)=>{
       console.log(res);
       this.myFunction();
    })