0

I had a problem with Angular 2 HTTP service call within for loop. The Loop run fast as compare to the service call return the result too slow.

   this.projectService.addProjectServices(pid,rid,vname,field_name,ser_name).subscribe(
        pdata => {
          for(var k=0;k<pdata['revision_info'][0].office_info[0].vertical_info[index].service_selection_info.length;k++) // Find service index
          { 
              if(ser_name==pdata['revision_info'][0].office_info[0].vertical_info[index].service_selection_info[k].service_name)
              {
                s_index=k;
              }
          } 
          for(var k=0;k<pdata['revision_info'][0].building_info.length;k++)
          {
              var bid=pdata['revision_info'][0].building_info[k]._id;
              // Checked Building only inserted
              if($("#"+bid+sid).prop("checked") == true)
              {
                var bid=$("#"+bid+sid).attr('build-id');
                var build_name=$("#"+bid+sid).attr('build-name');
                var service_name=$("#"+bid+sid).attr('service-name');
                alert(build_name);
                  // Update
                  for(var n=0;n<pdata['revision_info'][0].office_info.length;n++) // inserted at single office or both office
                  { 
                    var field_name='revision_info.$.office_info.'+n+'.vertical_info.'+index+'.service_selection_info.'+s_index+'.serviceselect_building_info';

                    projectServiceRef.projectService.updateProjectServices(pid,rid,vname,field_name,service_name,bid,build_name).subscribe(
                      pdata => {
                        alert("updated");
                    });
                  }  
              }
          }});  

In the above coding, first I made an HTTP service call to add a particular service document, then I found the index of the currently inserted service using for loop. The next for loop is to insert the subdocuments of the above service document. Here the second for loop run faster, but the subdocuments updated operation is too delay or some time update operation is not done. Anyone can help to solve my issue.

1 Answer 1

2

You can use bulk update functionality:

this.projectService.addProjectServices(pid, rid, vname, field_name, ser_name)
    .subscribe(pdata => {

        let _service_selection_info=pdata['revision_info'][0].office_info[0].vertical_info[index].service_selection_info;
        let _building_info=pdata['revision_info'][0].building_info;
        let _bulkQuery=[];


        // Find service index
        for (let k = 0; k < _service_selection_info.length; k++) {
            if (ser_name == _service_selection_info[k].service_name) {
                s_index = k;
            }
        }


        for (let k = 0; k < _building_info.length; k++) {
            let bid = _building_info[k]._id;
            let bidEle=$("#" + bid + sid);
            // Checked Building only inserted
            if (bidEle.prop("checked") == true) {
                let bid = bidEle.attr('build-id');
                let build_name = bidEle.attr('build-name');
                let service_name = bidEle.attr('service-name');
                alert(build_name);

                // Update
                for (let n = 0; n < pdata['revision_info'][0].office_info.length; n++) {
                    let field_name = 'revision_info.$.office_info.' + n + '.vertical_info.' + index + '.service_selection_info.' + s_index + '.serviceselect_building_info';

                    // Array of objects will used for bulk insert on server.
                    _bulkQuery.push({
                       pid:pid,
                       rid: rid,
                       vname: vname,
                       field_name:field_name,
                       service_name:service_name,
                       bid:bid,
                       build_name:build_name
                   })
                }
            }
         }

     projectServiceRef.projectService.updateProjectServices(_bulkQuery)
          .subscribe(pdata => {
              alert("updated");
          });
  });

Usefull link for bulk update are:

SOL:

https://www.codeproject.com/Articles/53669/Bulk-INSERT-UPDATE-DELETE-in-LINQ-to-SQL

https://www.aspsnippets.com/Articles/SqlBulkCopy--Bulk-Insert-records-and-Update-existing-rows-if-record-exists-using-C-and-VBNet.aspx

Mongodb:

https://docs.mongodb.com/manual/reference/method/Bulk.find.update/#Bulk.find.update

Example <logic for bulk update in mongo>:

    var bulk = db.projects.initializeUnorderedBulkOp();
    _bulkQuery.forEach(function (q) {
        bulk.find( { pid: q.pid } )
            .update( { $set: {
            rid: q.rid,
            vname: q.vname,
            field_name:q.field_name,
            service_name:q.service_name,
            bid:q.bid,
            build_name:q.build_name
        } } );
    });
    bulk.execute();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.