arpit joshi
arpit joshi

Reputation: 2134

Node js function called again and again

I am using nodejs express framework for my development. The web page has two buttons

1) Submit which calls the following function:

router.get('/record_enrich_quick/:quick', function(req, res) {
    console.trace();
    var json_struct={};
    json_struct["start_time"]=req.params.quick;
    json_struct["end_time"]="now";      
    json_struct["cookie"]=Math.random().toString();

    var data=JSON.stringify(json_struct);
    var args={
        data:data,
        headers: { "Content-Type": "application/json" }
    }
    //var Rest = require('node-rest-client').Client;
    //var client=new Rest();

    de_rest_client.post("http://localhost:8080/recordenrich",args,function(data,response){
        console.log("Received response from data enrich start");
    });
});

2) Refresh which calls the following function

router.get('/getDataEnrich/:from', function(req, res, next) {
    var fromValue = req.params.from;
    //Calls Elasticsearch and gets the data from it .
});

Issue:

When I click on submit a REST calls goes and it repeats again and again.

No way this call has gone from the front end ie: HTML. The reason is that I can't find the call on the Network side of the developer tools.

Also I have a debug point on the HTML side javascript where the function for "/record_enrich_quick/:quick" gets called, on the second time the debugger does not even point here. Which means there is some other place from where this function gets called multiple times.

To Summarize:

My function which has a rest call is called multiple times while the button which calls this function is pressed only once.

Is it something related to nodejs event queues? I am not sure.

Upvotes: 1

Views: 172

Answers (1)

arpit joshi
arpit joshi

Reputation: 2134

I finally found out the reason

My submit part of the code didnt have response .end .Below code solved my issue .Lesson was we cant keep a request hanging

router.get('/record_enrich_quick/:quick', function(req, res) {
        console.trace();
        var json_struct={};
        json_struct["start_time"]=req.params.quick;
        json_struct["end_time"]="now";      
       json_struct["cookie"]=Math.random().toString();

        var data=JSON.stringify(json_struct);
        var args={
                data:data,
                headers: { "Content-Type": "application/json" }
        }
        //var Rest = require('node-rest-client').Client;
        //var client=new Rest();
        de_rest_client.post("http://localhost:8080/recordenrich",args,function(data,response){
            res.end("reqest ends ");//This is was what made everything work
        });

});

Upvotes: 1

Related Questions