0

For some weird reasons, the push method is not adding the variable into the array.

this.errors.push, is working fine but this.tokenId its not working at all.

All I need is for that token to be sent to the client. I've tried doing res.json(token), its not sending it to the client. Now all I could think is to store that token in an array and then send all that user data to the client (which for now res.send(user) sends the data to the client but the this.tokenId stays empty even after trying to push it down in the code.

Console.log(token) prints the token in the console. What could be the reason that the token is not getting pushed to the array or sent to the client res.json(token) ?

In the other file (userController), this is the code for creating an instance of the user. Look Below

This is my code.

User.js

const UserSchema = require('./UserSchema');
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');

const jwt = require('jsonwebtoken');
const config = require('config');

let User = function (data) {
    this.data = data;
    this.errors = [];
    this.tokenId = [];
};

User.prototype.userExists = async function (req, res) {
    try {
        let userLookUp = await UserSchema.findOne({
            email: this.data.email,
        });
        if (userLookUp) {
            return this.errors.push('User already exists');
        }
        const avatar = gravatar.url(this.data.email, {
            s: '200',
            r: 'pg',
            d: 'mm',
        });

        userLookUp = new UserSchema({
            username: this.data.username,
            email: this.data.email,
            password: this.data.password,
            avatar: avatar,
        });

        const salt = await bcrypt.genSalt(10);

        userLookUp.password = await bcrypt.hash(this.data.password, salt);

        await userLookUp.save();

// starts from here

        const payload = {
            userLookUp: {
                id: userLookUp.id,
            },
        };

        console.log(payload);
        jwt.sign(
            payload,
            config.get('jwtSecret'),
            {
                expiresIn: 36000,
            },
            function (err, token) {
                if (token) {
                    tokenRetrieved = token;
                    console.log(tokenRetrieved);
                    this.tokenId.push(tokenRetrieved) // not pushing into the array above
                } else {
                    console.log('there is no token');
                }
            }
        );
    } catch (e) {
        console.log('there is a server problem');
    }
};

User.prototype.register = async function (req, res) {
    // Step #1: Validate user data
    this.cleanUp();
    this.validate();

    if (!this.errors.length) {
        await this.userExists();
    }

    // Step #2: See if user exists

    // Step #3: Get users gravatar

    // Step #4: Encrypt the password

    // Step #5: Return jsonwebtoken
    // Step #6: Only if there are no validation errors
    // then save the user data into a database
};

module.exports = User;

userController.js


exports.login = function () {};
exports.logout = function () {};
exports.register = async function (req, res) {
    let user = new User(req.body);
    await user.register();
    if (user.errors.length) {
        res.send(user.errors);
    } else {
        res.send(user);
    }
};
exports.home = function (req, res) {
    res.send('API up and running!');
};
Alaris
  • 57
  • 5

0 Answers0