【问题标题】:server gets `/customer/:id` instead of actual id服务器获取 `/customer/:id` 而不是实际的 id
【发布时间】:2019-02-02 11:59:50
【问题描述】:

前端

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 ,
});

【问题讨论】:

  • 我建议添加一个更清晰的标题,如果您能更好地用文字详细说明问题所在,这将有所帮助。通过这样做,更多的人将能够帮助您。
  • 您不应该在 $http 调用中传递实际客户 ID 来代替 :id 吗? $http.get('http://localhost:4000/customer/<customer id>')

标签: angularjs node.js mongodb mongoose


【解决方案1】:
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);
            });    
        }
);

【讨论】:

    【解决方案2】:

    尝试如下发送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);
                }); 
    
    );
    

    【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2019-05-08
    • 2016-11-14
    • 2021-10-08
    相关资源
    最近更新 更多