2

The request I make to my node api takes more than 4 minutes to respond. While the time the response is received. The angular app does not accept the response. On firebug, the url turns red.

How can I overcome this.

api.route('/allsearch')
    .post(function(req,res){
        var filters=req.body.everything;
        var filterid=req.body.filterId;
        var searchid=req.body.searchid;
        var zipgroup=req.body.zipgroup;
        var myObject = new Array();


        function getData(docs,  filters, filterid, callback) {
            function loop(i) {
                searchingalgo(docs[i], filters, filterid,  function(pers){
                        myObject[i] = pers;
                        if (i < docs.length) {
                        loop(i + 1);
                        } else {
                            callback();
                        }
                    });
            };
            loop(0);
        };//closing get data() 

        Searchradius.findOne({"searchid" : searchid, user: req.decoded.id}).exec(function(err, docs) {


            // Array to hold async tasks
            var asyncTasks = [];

            // Loop through some items
            zipgroup.forEach(function(item){
              // We don't actually execute the async action here
              // We add a function containing it to an array of "tasks"
              asyncTasks.push(function(callback){
                // Call an async function, often a save() to DB
                console.log(item);
                searchingalgo(item, filters, filterid,  function(pers){
                        myObject[item] = pers;
                      // Async call is done, alert via callback
                  callback();
                });
              });
            });


            Async.parallel(asyncTasks, function(){
                //console.log(myObject);
                Searchradius.update({ _id: searchid }, { $set: { ucounteds: myObject , uzips: zipgroup }}, function(err, result){
                    if(err) {
                        res.send(err);
                        return;
                    }
                    var fields = ['city', 'state', 'zip','distance', 'count'];
                    var myresults = [];
                    var tc=0;
                    var newMyobj= new Array();
                    co=0;
                    zipgroup.forEach(function(item){
                        tc+=myObject[item];
                        //myresults.push(jobj);
                    });

                    for(i=0;i<zipgroup.length;i++){
                        newMyobj[i]=myObject[zipgroup[i]];
                    }
                    console.log(tc);

                    Searchfilter.update({ _id: filterid }, { $set: { counted_results: tc }}, function(err, resultupdate){
                        //console.log(resultupdate);
                        //console.log(tc);
                    });




                //  console.log(myObject);
                //  console.log(newMyobj);
                    res.json({ 
                    success: true,
                    zips: zipgroup,
                    states: docs.states,
                    cities: docs.cities,
                    distances: docs.distances,
                    counted_results : newMyobj,
                    });

                });     //update searchradius
            });         //getdata function 
        });             //searchradius findone
    });

As requested, this is my node API. the zipgroup is a array of zips like

[37663, 37664, 37669, 37671, 37660, 37669, 37667, 37668, 37666, 37665, 37662, 37661]

Just to be clear the collection Consumer1s has more than 2900009876 documents. It is indexed properly and the query is taking the least time possible. But I am still facing this problem. Any suggestions would be helpful.

This is my post request from angular controller.

$http.post('/api/allsearch',
        {
            "everything":$scope.filterSearch ,
            "searchid":$routeParams.id, 
            "filterId": $scope.filterId, 
            "zipgroup" : $scope.zipgroup
        })
        .success(function(data){

            for(var i=0; i<data.zips.length;i++){
                oneset={
                    "zip": data.zips[i],
                    "state": data.states[i],
                    "city": data.cities[i],
                    "distance": data.distances[i],
                    "count": data.counted_results[i]
                };
                $scope.totalCount+=data.counted_results[i];
                $scope.results.push(oneset);
            }
            angular.forEach($scope.results, function (result) {
                    result.distance = parseFloat(result.distance);
                });
            $rootScope.processing=false;
            $scope.filterlinkdisplay=true;
    }); 
Satyam S
  • 267
  • 3
  • 18
  • @Satyam You need to add more details before getting any help like the error message, your code etc – Sachin Jain Jan 13 '16 at 08:36
  • added some code... Let me know if any other input is needed – Satyam S Jan 13 '16 at 08:51
  • Do you mean that Node.js request takes too long to be processed and AngularJS times out waiting for response? Are you looking to speed up Node.js code or handle slow processing properly in AngularJS? – krl Jan 13 '16 at 09:01
  • I want to handle the show processing properly in Amgular JS – Satyam S Jan 13 '16 at 10:00
  • @SatyamSaxena 1st option is polling: 1) AngularJS app does initial request 2) Node.js server issues a unique token for this request and starts working on collecting data 3) AngularJS app waits for several mins and does a request with previously received token to get data 4) If result is ready, you're done. If not, wait again and do another request from AngularJS. 2nd option is to use Websocket. It's a bi-directional protocol, so server can notify clients when data is ready. 3rd option is setting AngularJS $http timeout for 10 mins or so, so that AngularJS doesn't time out, waiting 4 mins. – krl Jan 13 '16 at 12:08
  • @SatyamSaxena see answer below – krl Jan 13 '16 at 12:30

1 Answers1

1

There are at least several options:

  1. set AngularJS $http timeout for 10 mins or so, so that AngularJS request doesn't time out, waiting for 4 mins to get the data
  2. polling: 1) AngularJS app does initial request 2) Node.js server issues a unique token to AngularJS app for this request and starts working on collecting data 3) AngularJS app waits for several mins and does a request with previously received token to get data 4) If result is ready, you're done. If not, wait again and do another request from AngularJS
  3. use WebSocket. On client side it is supported by many browsers, on server side use ws. It's a bi-directional protocol, so server can notify clients when data is ready.
krl
  • 5,087
  • 4
  • 36
  • 53
  • 1
    thanks for the answer... how can I achieve AngularJS $http timeout for 10 mins ?? – Satyam S Jan 13 '16 at 13:15
  • $http.post('/api/allsearch', { "everything":$scope.filterSearch , "searchid":$routeParams.id, "filterId": $scope.filterId, "zipgroup" : $scope.zipgroup }) .success(function(data){ angular.forEach($scope.results, function (result) { result.distance = parseFloat(result.distance); }); $rootScope.processing=false; $scope.filterlinkdisplay=true; }); – Satyam S Jan 13 '16 at 13:18
  • I added the POST request from my angular controller.. Please advice how can I set Timeout for 10 min in it. – Satyam S Jan 13 '16 at 13:20
  • `$http.post('/api/allsearch', { "everything":$scope.filterSearch , "searchid":$routeParams.id, "filterId": $scope.filterId, "zipgroup" : $scope.zipgroup }, {timeout:600000})`. Last parameter is configuration object, which can have timeout, specified in milliseconds. – krl Jan 13 '16 at 13:38
  • Help me out here please http://stackoverflow.com/questions/34851351/node-app-crashing-again-and-again-due-to-mongo-query – Satyam S Jan 18 '16 at 09:36