getting error - > TypeError : user.generateAuthToken is not a function -

getting error - > TypeError : user.generateAuthToken is not a function -

user.js__

const config = require('config');
const jwt = require('jsonwebtoken');
const  mongoose  = require("mongoose");
const Joi = require('joi'); 

const userSchema = new mongoose.Schema({
    name : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 50
    },
    email : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 50,
        unique : true
    },
    password : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 1024,
    }
});
const User = mongoose.model('User',userSchema);

userSchema.methods.generateAuthToken = function(){
    const token = jwt.sign({_id: this._id}, config.get('jwtPrivateKey'));
    return token;
}

function validateUser(user){
    const schema = {
        name     : Joi.string().min(5).max(50).required(),
        email    : Joi.string().min(5).max(255).required().email(),
        password : Joi.string().min(5).max(255).required(),
    };
    return Joi.validate(user,schema);
}

exports.User = User;
exports.validate =  validateUser;

users.js>__

const bcrypt = require('bcrypt');
const _ = require('lodash');
const {User, validate} = require('../models/user');
const mongoose = require('mongoose');
const express = require('express');
const Joi = require('joi');
const router  = express.Router();

router.post('/', async(req,res) => {

    const { error } = validate(req.body);
    if(error) return res.status(400).send(error.details[0].message);

    let user = await User.findOne({email : req.body.email});
    if(user) return res.status(400).send('User already registered.');

    user = new User(_.pick( req.body , ["name","email","password"]));
    const salt    = await bcrypt.genSalt(10);
    user.password = await bcrypt.hash(user.password , salt);

    await user.save();

    const token = user.generateAuthToken();
    res.send(token);
    res.header('x-auth-token',token).send(_.pick(user,['_id','name','email']));
});

module.exports = router;

我的应用程序的其余部分工作正常,但是当我生成授权令牌时,出现错误。任何人请让我知道我错过了什么。 我有 postel 我的模型(user.js)和路线(users.js)。

我是否可能因为安装任何旧的依赖项而收到此错误?

请查看我的代码并告诉我。

首先尝试提供更多代码,因为我们看不到您的架构。我认为你做的有点不对。创建模式时,将此模式传递给模型。我不确定你是否使用任何像 mongoose 这样的 ORM。

const userModel = mongoose.model('User', userSchema);

const newUser = new userModel();

//now try to run the method on model

const token = newUser.generateAuthToken();

您可以查看更多相关信息:

https://kb.objectrocket.com/mongo-db/how-to-add-instance-methods-with-mongoose-236

user.js__

const config = require('config');
const jwt = require('jsonwebtoken');
const  mongoose  = require("mongoose");
const Joi = require('joi'); 

const userSchema = new mongoose.Schema({
    name : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 50
    },
    email : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 50,
        unique : true
    },
    password : {     
        type : String,
        required  : true,
        minlength : 5,
        maxlength : 1024,
    }
});

// first assign method and then create model from it
userSchema.methods.generateAuthToken = function(){
    const token = jwt.sign({_id: this._id}, config.get('jwtPrivateKey'));
    return token;
}

const User = mongoose.model('User',userSchema);

function validateUser(user){
    const schema = {
        name     : Joi.string().min(5).max(50).required(),
        email    : Joi.string().min(5).max(255).required().email(),
        password : Joi.string().min(5).max(255).required(),
    };
    return Joi.validate(user,schema);
}

exports.User = User;
exports.validate =  validateUser;