SPFX - Call function after fetching data from multiple lists

Occasional Contributor

In my SPFX web part I have two functions:

 

SendAnEmilUsingPnpJs

and

GetAnotherData

 

And, I would like to call those functions after fetching data from multiple lists, but it doesn't work.

Entire code works fine until line "this.SendAnEmilUsingPnpJs(bossEmail);" where I get an error:

 

"Error: TypeError: Cannot read property 'SendAnEmilUsingPnpJs' of undefined"

 

Here is the code:

How can I call those functions after everything else executes?

pnp.sp.web.lists.getByTitle("First list")
    .items.select("Name", "MT/Title", "MT/ID").expand("MT").filter(`ProfilId eq ${userId}`).get().then(function(result) {            
      let MTId = result[0].MT.ID;
      console.log("MTId: " + MTId);
      pnp.sp.web.lists.getByTitle("Second list").items.filter(`MTId eq ${MTId}`).get().then(function(result) {      
        console.log("user: " + result[0].myUserId);

        pnp.sp.web.siteUsers.getById(userId).get().then(user => {
          console.log('Email ID: ', user.Email); 
          
          let size = $('#tbl_mytable tbody tr').length;
          
          $('#tbl_mytable tbody tr').each( (tr_idx,tr) => {      
            let tdElem = $(tr).children('td');                
           
              pnp.sp.web.lists.getByTitle('Third list').items.getById(parseInt(tdElem[12].innerText)).update(
              {
                Status: statusValue
              }).then((iar: ItemAddResult) => {
                if(size == tr_idx+1)
                {                 
                  let bossEmail = "mail@server.com"
                  this.SendAnEmilUsingPnpJs(bossEmail);
                  this.GetAnotherData();
                }
              }).catch((error:any) => {
                  console.log("Error: ", error);
              });
          });
          
        })
        
        
      }).catch(function(err) {
          alert(err);
      });      
    }).catch(function(err) {
        alert(err);
    });
	
	
	
public SendAnEmilUsingPnpJs(emailAsString): void 
  {    
      sp.utility.sendEmail({        
        Body: "test test",
        Subject: "test email",
        To: [emailAsString],
      }).then((i) => {

      }).catch((i) => {  
        
      });       
  }
  
 public GetAnotherData()
 {
 console.log('call');
 }
0 Replies