'user.toJSON is not a function

I'm trying to use authentication using passport and jwt, and when I use user.toJSON() to create tokens it give me following errorenter image description here

const User = require('../../../models/user');
const jwt = require('jsonwebtoken');


module.exports.createSession = async function(req, res){
    try{

        let user = User.findOne({email: req.body.email});

        if(!user || user.password != req.body.password){
            return req.json(422,{
                message: 'Invalid Username or Password'
            })
        }

        return res.json(200, {
            message: 'Sign in Successful, here is your token keep it safe',
            data:{
                token: jwt.sign(user.toJSON(), 'codeial', {expiresIn :'100000'})
            }
        })

    }catch(err){
        console.log('***********', err);
        return res.json(500,{
            message: 'Internal Server Error'
        })
    }
}

Anyone Can help me in resolving this error?



Solution 1:[1]

try just to remove the .toJSON

data:{
                token: jwt.sign(user, 'codeial', {expiresIn :'100000'})
            }
or

JSON.stringify(user);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ANDRIANIAINA