Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何通过Mongodb数据库在angular js中使用$http服务按名字获取用户_Angularjs - Fatal编程技术网

Angularjs 如何通过Mongodb数据库在angular js中使用$http服务按名字获取用户

Angularjs 如何通过Mongodb数据库在angular js中使用$http服务按名字获取用户,angularjs,Angularjs,您好,我正在尝试从我的mongoDB数据库中获取单个用户 我提供的服务如下: 服务: app.service('UsersService', function($http, $routeParams) { var getAllUrl = '/users'; this.getSingleUser = function() { return $http.get(getAllUrl/$routeParams.firstname) .then(function(

您好,我正在尝试从我的mongoDB数据库中获取单个用户

我提供的服务如下:

服务:

app.service('UsersService', function($http, $routeParams) {
  var getAllUrl = '/users';

  this.getSingleUser = function() {
    return $http.get(getAllUrl/$routeParams.firstname)
            .then(function(response) {
              return response.data;
            })
  }
});
app.controller('singleContactCtrl',
  function($scope, UsersService, $routeParams) {

   // Get single contact

   UsersService.getSingleUser().then(function(data) {
     $scope.singlecontact = data;
   });
});
然后是控制器

控制器:

app.service('UsersService', function($http, $routeParams) {
  var getAllUrl = '/users';

  this.getSingleUser = function() {
    return $http.get(getAllUrl/$routeParams.firstname)
            .then(function(response) {
              return response.data;
            })
  }
});
app.controller('singleContactCtrl',
  function($scope, UsersService, $routeParams) {

   // Get single contact

   UsersService.getSingleUser().then(function(data) {
     $scope.singlecontact = data;
   });
});

在我的api上,当我执行/users/firstname时,我会得到我的json数据。我做错什么了吗?使用该资源非常简单,但我想进一步了解$http的工作原理

我认为问题在于url
getAllUrl/$routeParams。firstname
应该改为
getAllUrl+'/'+$routeParams。firstname

我认为问题在于url
getAllUrl/$routeParams。firstname
应该改为
getAllUrl+'/'+$routeParams。firstname

你能做什么$scope.singlecontact=JSON.parse(数据);?非常感谢。我也试过了,但没有效果。返回404。这里至少有3处错误。1) $http.get需要一个字符串参数,您的参数是一个变量除以另一个变量。2) “getSingleUser”方法应该有一个参数(firstname),您应该使用arg从控制器调用该方法。首先试试这两件事。你能做$scope.singlecontact=JSON.parse(data);?非常感谢。我也试过了,但没有效果。返回404。这里至少有3处错误。1) $http.get需要一个字符串参数,您的参数是一个变量除以另一个变量。2) “getSingleUser”方法应该有一个参数(firstname),您应该使用arg从控制器调用该方法。首先试试这两件事。非常感谢这正是问题所在。非常感谢这正是问题所在