Mongodb + Angularjs:如何将_id从angular发布到服务器

时间:2017-06-16 19:50:31

标签: angularjs node.js mongodb mongoose mean-stack

我为我的project here使用mongodb,但我不确定如何将ObjectId从角度发布到节点后端。我在 GET 中看到问题_id但不确定如何将其发送回 POST 。我没看到。 api中的req.body要求问题和员工_id使用mongodb。



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

(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

更新:我已添加到此处。它说&#34;无法验证用户&#34;

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 个答案:

答案 0 :(得分:1)

您需要将_id从响应对象复制到$ scope.answer对象。您应该将代码更改为:

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