我正在使用JWT進行用戶授權。我使用Node API將數據插入到mongodb中。現在我想將登錄用戶的id和數據一起插入到mongodb中。
角
//factory for blog insert
app.factory('blogFactory', function($resource, $rootScope){
return $resource('/api/addblog/:id', {id:'@_id'},{get:{headers:{'authorization':$rootScope.token}}});
});
//controller for add blog
app.controller('blogpostCtrl', function($rootScope, $scope, blogFactory, $location){
$scope.addBlog=function(){
$scope.blogg=new blogFactory($scope.blog);
$scope.blogg.$save(function(){
$scope.blog=" ";
$scope.alert="Blog Successfully Inserted..!!!";
});
};
});
節點API
apiRoutes.post('/addblog', function(req, res){
var tokenx=req.headers.authorization;
console.log(tokenx);
var loggedinUser= jwt.decode(tokenx, config.secret);
var CurrentDate=Date.now();
var newBlog=new blogModel({
title:req.body.title,
description:req.body.description,
category:req.body.category,
date:CurrentDate,
by:loggedinUser._id
});
newBlog.save(function(err, data){
if(err){return res.json({success:false, msg:'Blog Not Posted'})}
else{
res.json({success:true, msg:'Successfully Posted'});
}
});
});
所以,我想知道的是,它是寫在$resource
headers
具有角JS的正確方法。 當我執行此代碼時,它顯示錯誤Error: No Token Supplied
。並在console.log
中也顯示一個錯誤POST http://localhost:3000/api/addblog 500 (Internal Server Error)
。
請幫忙。
你得到的'req.headers.authorization'值發送您的要求? – abdulbarik
沒有..它的'undefined' –
在角度方面你可以在發送過程中獲得'$ rootScope.token'這個值嗎? – abdulbarik