我正在使用MEAN棧的app
,我有用戶註冊並使用satellizer
登錄,一切正常。Nodejs通過編號獲取用戶
但是,當我嘗試get
用戶通過它的ID我什麼都沒得到,我可以做請求獲得所有用戶,但不是通過ID。
注意我使用Ionicframework作爲forntEnd框架。 這裏是我的後端終點代碼:
app.get('/api/me', function(req, res) {
User.findById(req.user, function(err, user) {
console.log(req.user);
console.log(user);
res.send(user);
});
})
我的前端代碼控制器:
.controller('ProfileCtrl', function($scope, $http, $stateParams, $ionicLoading, $timeout, $stateParams) {
$ionicLoading.show({
template: 'Loading...'
});
$http.get('http://localhost:3000/api/me')
.success(function(data) {
console.log("Recived data via HTTP", data);
$timeout(function() {
$ionicLoading.hide();
$scope.user = data;
}, 1000);
})
.error(function() {
console.log("Error while getting the data");
$ionicLoading.hide();
});
})
請求頭:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ar;q=0.6,fi;q=0.4,it;q=0.2,en-GB;q=0.2,en-CA;q=0.2
Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1NTAxYjAxMmExMjRlZjIwMTc4M2ExMTQiLCJleHAiOjE0MjcxMzk0NDV9.x9QEdE4E-Vh1SklCheiZqsnJsg8jGzdJnPx2RXeZqS8
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:8100
Referer:http://localhost:8100/
什麼'req.user'的'User.findById'調用之前的值? – JohnnyHK 2015-03-13 19:08:53
它是未定義的 – 2015-03-13 19:27:49