TypeError:User.findOne不是函数

时间:2019-02-17 20:11:39

标签: node.js angular mongodb

我正在制作一个api nodejs,我想用jwt登录,我已经创建了模型和路由,我正在用方法post在postman上对其进行测试,我上有一个名为“ user”的集合mongodb,但是当我发送请愿书时出现此错误`

TypeError: User.findOne is not a function
    at E:\AppAgricola\Proyecto\api\routes\loginRoutes.js:8:27
    at Layer.handle [as handle_request] (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\layer.js:95:5)
    at E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:335:12)
    at next (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:174:3)
    at router (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:317:13)
    at E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:335:12)
    at next (E:\AppAgricola\Proyecto\api\node_modules\express\lib\router\index.js:275:10)
    at app.use (E:\AppAgricola\Proyecto\api\app.js:27:5)`

我在邮递员上发送用户名和密码字段

                var express = require('express');
                var router = express.Router();
                var User = require('../models/user');
                var jwt = require('jsonwebtoken');

                router.post('/login', function(req, res, next) {

                    let promise= User.findOne({username:req.body.username}).exec();

                    promise.then(function(doc){
                        if(doc){
                            if(doc.isValid(req.body.password)){
                                let token = jwt.sign({username:doc.username}, 'secret', {expiresIn: '3h'});
                                return res.status(200).json(token);

                            }else{
                                return res.status(501).json({message: 'invalid credentials'});
                            }    
                        }else{
                            return res.status(501).json({message:'User email is not registered'})
                        }
                    });

                    promise.catch(function(err){
                        return res.status(501).json({message:'some internal error'})
                    })
                })

module.exports =路由器;

这是模型

var mongoose= require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');

var schema =  new Schema({
        username:{type:String, require:true},
        password:{type:String, require:true},
        //creation_dt:{type:String, require:true}
    },
);

schema.statics.hashPassword = function hashPassword(password){
    return bcrypt.hashSync(password, 10);
}

schema.methods.isValid = function(hashedpassword){
    return bcrypt.compareSync(hashedpassword, this.password);
}

module.export = mongoose.model('用户',模式); 我希望方法发布返回一个jwt


这是模型中的方法“ isValid”,如您所见,我有一个console.log(),向我显示了变量“ this.password”和“ hashedpassword”,该变量相等,并且此方法必须返回boolean = true但返回“ false”,我不知道会发生什么:

schema.methods.isValid = function(hashedpassword){
    console.log(this.password)
    console.log(hashedpassword)
    var data = bcrypt.compareSync(hashedpassword, this.password);
    console.log(data)
    return bcrypt.compareSync(hashedpassword, this.password);
}

4 个答案:

答案 0 :(得分:0)

module.export = mongoose.model('User', schema);不导出任何内容。

Node.js(不是ES6)导出为:

module.exports = ...;

“ require”功能的返回值在三点处都是。

答案 1 :(得分:0)

确保导出正确。

这对我有用:

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

答案 2 :(得分:0)

就我而言,我不得不改变

spatstat(由VSCode自动添加到导入中)

import User from '../../models/User';

希望这可以帮助案件相似的人。

答案 3 :(得分:-1)

只需使用console.log(module.exports),就像:

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

如果它打印模式,则说明它正常工作,并且此问题仅在module.exports上存在。