【问题标题】:Retrive data from mongodb in a MEAN stack web application based on email id used for authentication基于用于身份验证的电子邮件 ID 从 MEAN 堆栈 Web 应用程序中的 mongodb 检索数据
【发布时间】:2017-07-26 15:26:03
【问题描述】:

我无法根据身份验证时使用的电子邮件 ID 从 mongodb 检索数据。我正在创建一个简单的网络应用程序,用户可以在其中捐赠他们的书籍。

用户必须首先使用电子邮件和密码凭据登录应用程序。 当用户捐赠一本书时,数据会以下列方式(angularjs)存储在 mongodb 中名为“books”的表中:

    Description of book: 
      <form id="myForm" name="myForm" action="" method="">
        <table class="responstable">
        <tr><td>Book's name:</td>
        <td><input type="text" ng-model="don.donor"/></td></tr>

        <tr><td>Donor's phone number:</td>
        <td><input type="text" ng-model="don.phone" /></td></tr>

        <tr><td>Your email_id</td>
        <td ng-init="des.email = '<%= user.local.email %>'">{{don.email}}</td></tr>

        </table><br>
        <center><input id="check" name="check"type="submit" value="Donate" ng-click="donate();"/></center>
      </form>

用户的电子邮件 ID 会在他每次捐赠一本书时被存储。

这是 server.js 文件中的代码:

    var dbdes= mongojs('books' , ['books']);

    app.post('/books', function(req, res){
        console.log(req.body);
        dbdes.books.insert(req.body, function(err, doc){
            res.json(doc);
        })
    });

这是controller.js文件中的代码:

    $scope.donate = function(event){
        console.log($scope.don);
        event.preventDefault();
        $http.post('/books', $scope.don).success(function(response){
            console.log(response);
        });
    };

该应用有另一个标签历史记录。在点击历史记录时,用户必须能够查看他捐赠的所有书籍的详细信息。

历史按钮:

<a ng-click=" gethistory('<%= user.local.email %>');" ></span><span class="r">History</a>

我正在使用下表:

    <table> 
        <tr>    <td>Name of the book:</td>  </tr>                               
        <tr ng-repeat="y in books">
        <td>{{y.name}}</td> </tr>
    </table>

以下代码正在正确获取电子邮件(如控制台显示:GET /books/srinidhiraichur1234@gmail.com 304 14ms),但条目未显示在网页上。为了检索用户的个人捐赠,我应该对服务器和控制器文件进行哪些更改。

server.js:

app.get('/books', function (req , res){

dbdes.books.find(function(err,docs){
    console.log(docs);
    res.json(docs);
});

});

controller.js:

    $scope.gethistory = function(email){
    console.log(email);

    $http.get('/books/' + email).success(function(response){
        //this line is printing. but i cant see the details on the web page
        console.log("i got the data requested - history");
        $scope.books = response;
        $scope.y = "";
    })
};

【问题讨论】:

  • 你如何验证你的用户?
  • 注册时,用户必须输入他的emailid和密码,这些密码存储在一个表格中。虽然捐赠用户的电子邮件 ID 完美地存储在字段名称电子邮件下的书籍中。@Varedis

标签: angularjs node.js mongodb express mongoose


【解决方案1】:

你需要更新你的get book功能

app.get('/books/:email', function (req , res){
  var email = req.params.email;
  dbdes.books.find({
    "email": email
  }, function(err,docs){
    console.log(docs);
    res.json(docs);
  });
});

【讨论】:

  • 它仍然显示所有条目。我需要根据用户的 emailid 过滤条目。
  • 现在检查答案以根据用户的电子邮件 ID 过滤条目。
  • 我收到此错误:TypeError: dbdes.books.find(...).exec 不是函数。由于同一张表有两个 get 函数,因此第二个 get 函数没有执行。
  • 你用哪个插件连接mongodb?
  • 我正在使用猫鼬。我对问题中的代码进行了一些更改。请看一看。
猜你喜欢
  • 2019-07-28
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2010-11-22
  • 2020-05-04
相关资源
最近更新 更多