Angularjs将变量从控制器传递到dao服务

时间:2014-03-24 11:30:11

标签: node.js angularjs mongodb mongoose

我正在尝试将一个变量从控制器传递到Angularjs应用程序(前端)和nodejs后端的DAO服务。

这是我的控制器:

    $scope.afficherProfils = function() {
    $http.get(configuration.URL_REQUEST + '/profile')
        .success(function(data) {
            $scope.owner = data._id;
            console.log('$scope.owner =========>');
            console.log($scope.owner);
            $http.get(configuration.URL_REQUEST + '/listerProfil', {
                owner: $scope.owner
            })
                .success(function(data) {
                    console.log('$scope.listeProfils =========>');
                    console.log(data);
                    $scope.listeProfils = data;
                });
        });
};

我正在调用/ profile以获取已添加配置文件的帐户的_id,之后我调用/ listerProfil成功并传递所有者参数。

在我的DAO服务中,代码如下:

 exports.all = function(req, res) {
  console.log('req.body ================================>');
  console.log(req.body);
  Profil.find({
    owner: req.body.owner
  }, function(err, profils) {
    if (err) {
      res.send({
        'result': 'error'
      });
    } else {
      res.send(profils);
    }
  });
};

我不知道为什么当我做console.log时我的req.body是空的。

任何想法?

1 个答案:

答案 0 :(得分:1)

HTTP-CRUD(Create- $ http.post,读 - $ http.get,更新 - $ http.put,Dlete- $ http.delet)

$http.get-It used to get the value means this body contain empty value

$http.post-It used to create value means this body contain data (your are post some data to server)

$http.update-It used to update exit value this body also contain data

$http.delete-It used to delete exit value this body contain empty(send through param(?id=1111))

将代码http.get更改为http.post