0

Resource:

This is the Resource I use for the Update function. I am unable to invoke the Update method in my Resource (written in Java)

c.adminCompaniesResource = $resource("api/admin/companies/:id",{"id":"@id"},{
  "updateCompany": {method:"PUT"}
});

Update Company Function:

First I put all companies in an Array and then I'm trying to invoke an update action on a specific company in the Array

Problem with line: "updateCompany.$updateCompany(company, function() {"

TypeError: updateCompany is undefined

c.allCompanies = [];

c.updateCompanyForm = function(company) {
  const updateCompany = company;
  c.allCompanies = c.adminAllCompaniesResource.query(c.companyFields, function() {
    c.allCompanies.forEach(function(company){
      if (c.companyFields.id == company.id) {
        company.id = c.companyFields.id;
        company.password = c.companyFields.password;
        company.email = c.companyFields.email;
        updateCompany.$updateCompany(company, function() { 
          //problem here
          c.companyFields.id = company.id;
          c.companyFields.compName = company.compName;
          c.companyFields.password = company.password;
          c.companyFields.email = company.email;
          c.updateCompanyTableDiv = true;
          c.updateCompanyExceptionDiv = false;
          }, function() {
            c.error("Request could not be completed");
            c.updateCompanyExceptionDiv = true;
            c.updateCompanyTableDiv = false;
          });
        }
      })
   }, function() {
        c.error("Request could not be completed")
        c.getAllCompaniesExceptionDiv = true;
        c.getAllCompaniesTableDiv = false;
   });
}
elvis_ferns
  • 524
  • 6
  • 14
Guy BD
  • 69
  • 1
  • 3
  • 10
  • It looks a problem similar to [this](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) question – Bulkan Feb 13 '18 at 12:19
  • I looked at what you suggested, My code already contains one of the presented solutions: forEach loop. I don't believe what you suggested is similar to my problem. Can you please review my post again? I am stuck on this for more than two weeks – Guy BD Feb 13 '18 at 17:59

1 Answers1

0

There seems to be no problem with your code. The error message is pretty much for the resolution of the issue. The call to function c.updateCompanyForm() is sending the undefined value to it. Which leads to failure. If you can provide the complete code fragment, than it is possible to find the source of error.

I tried to mock up your code with dummy function.

<html>
   <head>
      <title>Test</title>
   </head>
   <body>
      <script>
         var c = {
            companyFields: {
                id: 10
            },
            adminAllCompaniesResource: {
                query: function(fields, callback) {
                    callback();
                    return [{
                        id: 10
                    }];
                }
            }
         };
         c.allCompanies = [
            {id: 10}
         ];

         c.updateCompanyForm = function(company) {
           const updateCompany = company;
           c.allCompanies = c.adminAllCompaniesResource.query(c.companyFields, function() {
             c.allCompanies.forEach(function(company){
               if (c.companyFields.id == company.id) {
                 company.id = c.companyFields.id;
                 company.password = c.companyFields.password;
                 company.email = c.companyFields.email;
                 updateCompany.$updateCompany(company, function() { 
                   //problem here
                   c.companyFields.id = company.id;
                   c.companyFields.compName = company.compName;
                   c.companyFields.password = company.password;
                   c.companyFields.email = company.email;
                   c.updateCompanyTableDiv = true;
                   c.updateCompanyExceptionDiv = false;
                   }, function() {
                     c.error("Request could not be completed");
                     c.updateCompanyExceptionDiv = true;
                     c.updateCompanyTableDiv = false;
                   });
                 }
               })
            }, function() {
                 c.error("Request could not be completed")
                 c.getAllCompaniesExceptionDiv = true;
                 c.getAllCompaniesTableDiv = false;
            });
         }
         //Test 1: This should show message "Here"
         c.updateCompanyForm({id: 10, $updateCompany: function() {alert('Here')}});
         //Test 2: This will produce error
         c.updateCompanyForm(undefined);
      </script>
   </body>
</html>

Hopefully this gives you the idea from where the issue started propagation.

Kamal Singh
  • 990
  • 8
  • 13