【问题标题】:Mongodb + Angularjs: How to POST _id from angular to the serverMongodb + Angularjs:如何将_id从角度发布到服务器
【发布时间】:2017-06-17 06:40:06
【问题描述】:

我正在为我的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>
(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 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
        };
    });
});

更新:我已在此处添加我的 get。 它显示“无法验证用户”

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);
                });
        }
    });

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我实际上想出了如何获取问题 ID。就这么简单。我现在正在努力寻找用户 ID。我如何使用用户的令牌来做到这一点?用户当前已登录。
    • 在 User.findOne(... 之后,你就有了用户对象。你需要将 user._id 从 node.js 传递给 Angular 代码。
    • 我在上面的问题中添加了我的 GET。它返回“无法验证用户”,尽管它以角度显示用户已登录。console.log(user); 也显示null 简而言之,POST 会选择用户,而 GET 不会
    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多