2017-06-16 2 views
0

私はproject hereのためにmongodbを使用していますが、ObjectIdの角度からノードのバックエンドへの投稿方法は不明です。 GETの質問_idが表示されますが、POSTに返信する方法がわかりません。私はそれを見ない。 API内のreq.bodyは、質問と従業員_idの両方がmongodbで作業する必要があります。Mongodb + Angularjs:サーバーから角に_idする方法

<h1 class="question capitalize">{{answer.question}}</h1><br> 
 
<a class="btn btn-ghost" ng-click="yes(answer)" href="#">Yes</a>

(function() { 

"use strict"; 

angular 
    .module("feedback") 
    .controller("feedbackCtrl", function($scope, $http, Auth){ 
     if (Auth.isLoggedIn()){ 
      Auth.getUser().then(function(data){ 
       $scope.answer = {}; 
       $http.get('/feedback').then(function(response){ 
        $scope.answer.question = response.data.title; 
       }); 

       $scope.yes = function(info) {     
        $scope.answer.question = info.question; 
        $scope.answer.response = 'Yes'; 
        $scope.answer.username = data.data.username; //from auth 
        $http.post('/feedback', $scope.answer).then(function(response) { 
         console.log(response); 
        }); 
       }; 

      }); 
     } 
     else { 
      console.log('Failure: User is NOT logged in'); 
     } 
    }); 

})(); 

API

router.post('/', function (req, res) { 
    User.findOne({ username: req.body.username }).exec(function(err, user) { 
     if(err) throw err; 

     if(!user) { 
      res.json({ success: false, message: 'Could not authenticate user' }) 
     } else if (user){ 
       Answer.findOneAndUpdate({ 
          question: req.body.question, 
          employee: req.body.username 
         }, 
         req.body, 
         { 
          upsert: true //updates if present, else inserts 
         }) 
       .exec(function(err, answer) { 

       }); 
       console.log('entry saved to answer >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); 
     }; 
    }); 
}); 

enter image description here

enter image description here

更新:ここに私の所持を追加しました。それはあなたが$ scope.answerオブジェクトに応答オブジェクトから_idをコピーする必要が

router.get('/', function(req, res){ 
    User.findOne({ username: req.body.username }).exec(function(err, user) {  
     console.log(user); 
     if(err) throw err; 

     if(!user) { 

      res.json({ success: false, message: 'Could not authenticate user' }) 
     } else if (user) { 

      // continue with processing, user was authenticated 
      token = jwt.sign({ username: user.username }, secret, { expiresIn: '24h'}); 

      res.json({ success: true, message: 'User authenticated', token: token }); 

       Question.findOne({ title:"Should we buy a coffee machine?" }) 
         .exec(function (err, docs){ 
         console.log(docs); 
         res.json(docs); 
       }); 
     } 
    }); 

答えて

1

「ユーザーを認証できませんでした」と言います。コードを次のように変更する必要があります。

$scope.answer.question = response.data.title; 
$scope.answer._id = response.data._id; 
+0

はい私は質問IDを取得する方法を実際に考え出しました。それは簡単でした。私は今、ユーザーIDを見つけるのに苦労しています。これを行うには、ユーザーのトークンをどのように使用すればよいですか?ユーザーは現在ログインしています。 –

+0

User.findOne(...の後に、ユーザーオブジェクトを持っています。user._idをnode.jsから角度コードに渡す必要があります。 –

+0

ユーザがログインしている角度で表示されていますが、「ユーザを認証できませんでした」と返す。また、 'console.log(user);'は 'null'を表示する要するに、POSTはGET中にユーザをピックアップするしない –

関連する問題