服务器获取`/ customer /:id`而不是实际ID

时间:2018-08-27 22:50:47

标签: angularjs node.js mongodb mongoose

前端

customer.controller("single_customer", 
     function($scope, $http, $routeParams)
        {    
            $http.get('http://localhost:4000/customer/:id').then (function(data){
                // $scope.whichCustomer = $routeParams.id;
                $scope.customer = data;

                console.log($scope.customer)

            }).catch(function(err){
                    console.log(err);
            }); 


        }
);

后端

app.get("/customer/:id", (req, res) => {

    var user = String(req.params.id);
    console.log(user)

    Customers.find({id:user}, (err, items) => {
        if (err) res.status(500).send(err)

        res.status(200).send(items);
        console.log(items)
      });
    // console.log(Customers.find({id:user}));
    // res.send(Customers.find({id:user}));

   });

猫鼬模式

var Customers = mongoose.model('Customers',{
    id: {type:String , required:true} ,
    name: String ,
    city : String ,
    state : String ,
    gender : String ,
});

2 个答案:

答案 0 :(得分:0)

customer.controller("single_customer", 
     function($scope, $http, $routeParams)
        {
            // $scope.whichCustomer = $routeParams.id;
            var base = "http://localhost:4000/customer/"
            var url = base + $routeParams.id;   
            $http.get(url).then (function(response){
                $scope.customer = response.data;  
                console.log($scope.customer)

            }).catch(function(err){
                    console.log(err);
            });    
        }
);

答案 1 :(得分:0)

尝试发送ID,如下所示:

customer.controller("single_customer", 
     function($scope, $http, $routeParams)
        {    
       $http.get("http://localhost:4000/customer/"+$routeParams.id}).then (function(data){
                // $scope.whichCustomer = $routeParams.id;
                $scope.customer = data;

                console.log($scope.customer)

            }).catch(function(err){
                    console.log(err);
            }); 

);