简体   繁体   中英

keep getting HTTP response code “1” for HTTP REQUEST

i was trying to make a http request to delete a post everything work fine but the response code i got back is always "1" and i don't know why.thus, this affect my front end as well for the error handling part. may i know why is this happened ? 错误代码

this is what i have so far

register the http request

app.delete('/channelHandler/channel/:permalink/delete', channelHandler.deleteChannelByPermalink);

the route

after execute i got "no problem in route"

this.deleteChannelByPermalink = function(req, res, next){
      var permalink = req.params.permalink;
    channels.deleteChannelByPermalink(permalink,function(err, results) {
            "use strict";
            if(err){
            console.log("error in route");
               res.send(400,err);
            }
            console.log("no problem in route");
             res.send(200,results);
        });
}

the database

after execute i got "done in database"

    this.deleteChannelByPermalink = function(data, callback){
         "use strict"
            channels.remove({'channelPermalink': data}, function(err, post) {
                "use strict";
                if (err) return callback(err, null);
                callback(err, post);
                console.log("done in database");
            });
    }

the front end

//after execute i got "something is wrong" instead of "LOL"

 $scope.confirm = function(titleform){
     if(titleform === items.currentTitle){
       $http.delete('/channelHandler/channel/'+items.currentPermalink+'/delete').
                        success(function(data){
                          alert("LOL")
                        }).error(function(err){
                           alert("Something is wrong")
                           $scope.errorMessage = err;
                        });
                    $modalInstance.dismiss('cancel');
                    }else{
                      $scope.errorMessage = "Please enter the correct title "
                    }
                  } 

Your error handling control flow is missing an essential "return", which causes you to call res.send twice for the same request, which is a bug.

this.deleteChannelByPermalink = function(req, res, next){
  var permalink = req.params.permalink;
  channels.deleteChannelByPermalink(permalink,function(err, results) {
    "use strict";
     if(err){
       console.log("error in route");
       res.send(400,err);
       //NEED TO RETURN HERE!
       return;
     }
     console.log("no problem in route");
     res.send(200,results);
    });
}

However, based on the information in your question I'm not certain this mistake is actually causing the behavior you are seeing. But you need to fix this in any case. Otherwise I don't see any other obvious errors in your snippets.

it was a silly mistake i made, i'm so sorry for wasting your time.

the mistake i made was i return the result back to the front end, the result contain the value "1" which is the result for the database part

 channels.remove({'channelPermalink': data}, function(err, post) {
                "use strict";
                if (err) return callback(err, null);
                callback(err, post); // post contain "1"
                console.log("done in database");
            });

this is my correction

channels.deleteChannelByPermalink(permalink,function(err, results) {
            "use strict";
            if(err){
            console.log("error in route");
               res.send(400,err);
               return;

            }
            console.log("no problem in route");
             // res.send(200);  instead of res.send(200,results)
             return;
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM