Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing props not working in a nested function

When I pass the props to the OnBeforeSend function it works, but once I put it inside the nested args.ajaxsettings function it does not work.

export default class FileManager extends React.PureComponent {
  
  hostUrl = "https://amazons3.azurewebsites.net/api/AmazonS3Provider/";

  constructor(props) {
    super(props);
    this.fileSelection= this.fileSelection.bind(this);
  } 
  

  onBeforeSend(args) {  
    //this works
    console.log(this.props.team_id);

    args.ajaxSettings.beforeSend = function (args) {
      
      //this one doesn't work
      console.log(this.props.team_id);


      args.httpRequest.setRequestHeader('Authorization', 'public/');

    };

  }

In the console, it returns the error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'team_id')

Any guidance on how to get the second console.log(this.props.team_id); to work would be greatly appreciated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using this in a scope function call. Try using an arrow function instead:

args.ajaxSettings.beforeSend = (args) => {
  
  //this one doesn't work
  console.log(this.props.team_id);


  args.httpRequest.setRequestHeader('Authorization', 'public/');

};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading