简体   繁体   English

变量被传递而不使其后端

[英]Variable being passed not making it to backend

I'm trying to make a simple deletion from an array of objects. 我正在尝试从对象数组中进行简单删除。 I'm passing the object's id to the backend, but when I output the parameter to the terminal, it just prints an empty array. 我将对象的ID传递给后端,但是当我将参数输出到终端时,它只是打印一个空数组。

My Angular is passing the object to the server: 我的Angular正在将对象传递到服务器:

$scope.removeFavoriteRecipe = function(favorite){
    var deleteIt = confirm("Are you sure you want to remove this recipe from your favorites?");
    if(deleteIt){
        console.log(favorite._id)
        $http.delete("/removeFavoriteRecipe/" + favorite._id)
        .then(function(returnData){
            if(returnData.data.err){
                $scope.err = returnData.data.err;
            }else{
                $scope.err = "";
                $window.location.reload();
            }
        })
    }
}

I'm using server.delete("/removeFavoriteRecipe/:id",favRecipeController.removeFavoriteRecipe); 我正在使用server.delete("/removeFavoriteRecipe/:id",favRecipeController.removeFavoriteRecipe); to pass it to the backend 传递给后端

This is where I believe the hiccup happens. 我相信这是发生打happens的地方。 Once it gets to the backend, the id is not in req.params : 一旦到达后端,该id便不在req.params

var removeFavoriteRecipe = function(req, res){
    console.log(req.params._id);
    if(req.user){
        userModel.User.update({_id: req.user._id}, {$pull :{favoriteRecipes: {_id: req.params._id}}}, function(err){
            if(err){
                res.send(err);
            }else{
                res.send("success!");
            }
        }); 
    }else{
        var err = {err: "You are not logged in."};
        res.send(err);
    }
}

console.log(favorite._id); shows the id, but console.log(req.params._id); 显示ID,但显示console.log(req.params._id); shows an undefined variable. 显示未定义的变量。

EDIT: DELETE doesn't allow for body data, so you can either change your request to a PUT and use the code below, or use the original code and get the id from the url. 编辑: DELETE不允许正文数据,因此您可以将请求更改为PUT并使用下面的代码,或者使用原始代码并从URL获取ID。

Req.body isn't available immediately you need to add it to an string chuck by chuck and then create an object out of it once the stream ends: req.body不能立即使用,您需要将它添加到chuck中,然后在流结束后用它创建一个对象:

var removeFavoriteRecipe = function(req, res){

    let data = "";
    req.on('data', function(chunk) {
      // append the current chunk of data to the fullBody variable
      data += chunk.toString();

    });

    if(req.user){
        req.on('end', function() {
            let body = JSON.parse(data)
            userModel.User.update({_id: req.user._id}, {$pull :{favoriteRecipes: {_id: body._id}}}, function(err){
                if(err){
                    res.send(err);
                }else{
                    res.send("success!");
                }
            }); 
        });

    }else{
        var err = {err: "You are not logged in."};
        res.send(err);
    }
}

Figured out the problem: I should use put instead of delete , but also I was sending back favorite._id when I should have just sent favorite . 弄清楚了问题:我应该使用put而不是delete ,但是当我应该只发送favorite favorite._id时,我也要发送回favorite

This is my new code: 这是我的新代码:

Angular: 角度:

$scope.removeFavoriteRecipe = function(favorite){
    var deleteIt = confirm("Are you sure you want to remove this recipe from your favorites?");
    if(deleteIt){
        console.log(favorite._id)
        $http.put("/removeFavoriteRecipe", favorite)
        .then(function(returnData){
            if(returnData.data.err){
                $scope.err = returnData.data.err;
            }else{
                $scope.err = "";
                $window.location.reload();
            }
        })
    }
}

Server: 服务器:

server.put("/removeFavoriteRecipe", favRecipeController.removeFavoriteRecipe);  

Node: 节点:

var removeFavoriteRecipe = function(req, res){
    console.log(req.body._id);
    if(req.user){
        userModel.User.update({_id: req.user._id}, {$pull :{favoriteRecipes: {_id: req.body._id}}}, function(err){
            if(err){
                res.send(err);
            }else{
                res.send("success!");
            }
        }); 
    }else{
        var err = {err: "You are not logged in."};
        res.send(err);
    }
}

Special thanks to bstockwell for helping me get to my conclusion! 特别感谢bstockwell帮助我得出结论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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