Mongoose findOne函数无法使用身份验证

时间:2018-04-26 08:13:19

标签: node.js mongodb reactjs express mongoose

我正在制作一个模型:

// user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');

// Define collection and schema for Users
let User = new Schema(
  {
    firstName: String,
    lastName: String,
    emailaddress: String,
    password: String,
  },
  {
    collection: 'users'
  }
);

// authenticate input against database documents
User.statics.authenticate = ((emailaddress, password, callback) => {
  User.findOne({ emailaddress: emailaddress })
      .exec(function(error, user){
        if(error){
          return callback(error)
        } else if (!user){
          console.log('User not found!');
        }
        bycrypt.compare(password, user.password, function(err, result){
          if(result === true){
            return callback(null, user);
          } else {
            return callback();
          }
        })
      })
});

module.exports = mongoose.model('User', User);

正如您在我的模型上看到的,我将User.statics.authenticate放在我的代码上进行一些身份验证。然后在我的login.js路由文件中:

const path = require('path');
const express = require('express');
const router = express.Router();
const db = require('../../database/index');
const axios = require('axios');
const User = require('../../database/models/user');

router.get('/', (req, res) => {
  console.log('hi there this is working login get');
});

router.post('/', (req, res) => {
  var emailaddress = req.body.emailaddress;
  var password = req.body.password;


  if( emailaddress && password ){
    User.authenticate(emailaddress, password, function(err, user){
       if(err || !user){
         console.log('Wrong email or password!');
       } else {
         req.session.userId = user._id;
         return res.redirect('/');
       }
    });
  } else {
     console.log('both fields are required...');
  }

});

module.exports = router;

我调用了函数,然后调用了User.authenticate函数,并且我创建了根w / c的路由是我要保护的示例,并在登录后重定向用户:

router.get('/', (req, res) => {
  if(! req.session.userId ){
    console.log('You are not authorized to view this page!');
  }

  User.findById(req.session.userId)
      .exect((err, user) => {
        if(err){
          console.log(err)
        } else {
          res.redirect('/');
        }
      })
});

点击我的反应表单上的提交后,它会返回以下错误:

TypeError: User.findOne is not a function
    at Function.User.statics.authenticate (/Users/mac/Documents/monkeys/database/models/user.js:35:8)

我检查了Mongoose文档,似乎我正在使用正确的语法。任何想法我在这里做错了什么?请帮忙!对不起超级初学者!

PS。我已经安装并设置了基本快速会话。

更新

我从调用中移除箭头函数并使用this.model.findOne但仍然得到typerror findOne不是函数

// authenticate input against database documents
User.statics.authenticate = function(emailaddress, password, callback){
  this.model.findOne({ emailaddress: emailaddress })
      .exec(function(error, user){
        if(error){
          return callback(error)
        } else if (!user){
          console.log('User not found!');
        }
        bycrypt.compare(password, user.password, function(err, result){
          if(result === true){
            return callback(null, user);
          } else {
            return callback();
          }
        })
      })
};

1 个答案:

答案 0 :(得分:0)

findOne是您的User模型上的方法,而不是您的用户模型实例。它通过回调向调用者提供异步结果:

User.findOne({field:'value'}, function(err, doc) { ... });