Michell
Michell

Reputation: 13

ajax get request in node js express

I'm working a litlle bit on my Node js Skills.

I would like to add some data to mongodb using a button click.

Client side code looks like this:

        $(function() {
        $('#add').click(function(){
            $.ajax({
                type: "GET",
                url: "/item/<%= products._id %>/add"
            }).done (function (data) {
                alert(data);
                console.log(data);
            });
        });
    });

  <button type="submit" id="add" class="btn btn-primary">Interessted</button>

Server side code like this:

    app.get('/item/:id/add', function(req, res) {

    Listing.findByIdAndUpdate(
        { _id : req.params.id},
        { $push : {"product.interessteduser": req.user._id }},
        {  safe: true, upsert: true},
        function(err, model) {
            if(err){
                console.log(err);
            }

        });
});

The code works perfectly for me, but if I wait a little bit I get another request in my console.

Looks like this:

GET /item/557eec02aa1046b805190207/add 200 120001ms
GET /item/557eeb82aa1046b805190206/add 200 120000ms

So every time request /item/:id/add and wait for 120000ms I get another request. How to stop this?

I would like to hit the button once do the /item/557eeb82aa1046b805190206/add Get request and that's all.

Upvotes: 1

Views: 2430

Answers (3)

jenil christo
jenil christo

Reputation: 674

Add res.end(); or return false; to your code

Try something like this

    app.get('/item/:id/add', function(req, res) {

    Listing.findByIdAndUpdate(
        { _id : req.params.id},
        { $push : {"product.interessteduser": req.user._id }},
        {  safe: true, upsert: true},
        function(err, model) {
            if(err){
                console.log(err);
            }else{
             res.json({'status':'200','message':'Success!'});
             res.end();
             return false;
             }

        });
   });

Upvotes: 0

Sachin Shukla
Sachin Shukla

Reputation: 171

as you got the answer, however, its always elegant to write separate functions to deal with sending the success/failure case so that once tested it should be used and would avoid these errors.


  --Expose these two helper methods as module and always use that module to send the result or error response at last.

    exports.send_success = function (res, data) {
        res.writeHead(200, {"Content-Type": "application/json"});
        var output = { error: null, data: data };
        res.end(JSON.stringify(output));
    };

    exports.send_failure = function (res, err) {
        var code = exports.http_code_for_error(err);
        res.writeHead(code, { "Content-Type" : "application/json" });
        res.end(exports.error_for_resp(err));
    };

    function (err, results) {
            if (err) {
                helper.send_failure(res, err);
            } else {
                helper.send_success(res, results);
            }
    }

Upvotes: 0

Aaron Dufour
Aaron Dufour

Reputation: 17505

You never respond to the request. The browser retries the request after two minutes if you don't respond. Try something like

res.send("success");

Upvotes: 4

Related Questions