【问题标题】:Check if user's email exists in db检查用户的电子邮件是否存在于数据库中
【发布时间】:2016-03-11 17:12:16
【问题描述】:

我有一个基本的登录表单,想验证用户的电子邮件是否存在于数据库中,但不确定 angular + node 的语法。

Main.js 我有一个运行此功能的提交按钮上的 ng-click。我从输入中收到电子邮件,不知何故需要将其传递给检查数据库?

$scope.logIn = function() {
    var email = $scope.formInfo.email;

    $http.get('/findUser').success(function(response){
        console.log('find user data');
        console.log(response);
    });
};

Server.js 我有到数据库的连接,但不确定如何与客户端和后端数据建立连接或语法是什么

app.get('/findUser', function(req, res){
//what do I do here?
    db.rejoin_your_ex.find({ email: 'the user's email' }, function(err, docs){
        res.json(docs);
    });
});

【问题讨论】:

    标签: javascript angularjs node.js mongodb authentication


    【解决方案1】:

    客户端

    $scope.logIn = function() {
       var email = $scope.formInfo.email;
    
       $http({
           url: '/findUser', 
           method: "GET",
           params: {"email": email}
       }).success(function(response){
           console.log('find user data');
           console.log(response);
       });
    };
    

    服务器端

    在后端,您可以使用 Mongoskin 模块进行特定于 mongodb 的查询。

    var mongo = require('mongoskin');
    var db = mongo.db("mongodb://localhost:27017/test");
    db.bind('user');
    
    app.get('/findUser', function(req, res){
        //what do I do here?
        db.user.find({ email: req.params.email }, function(err, user){
            res.json(user);
        });
    });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 2012-03-13
      • 2019-06-29
      • 2019-09-17
      • 2014-03-15
      • 1970-01-01
      • 2019-07-13
      • 2021-04-24
      相关资源
      最近更新 更多