0

I am having a problem while making a controller in which there us a userController class which has login and createuser methods and I am trying to access the method I described from the this object but I cannot access the this.userExists() method. When I try to access, it shows that the userExists is undefined.

import User from '../models/User';
class userController {
    userExists(username) {
        User.findOne({ username }, (err, user) => {
            if (!!user) {
                return true;
            } else {
                return false;
            }
        });
    }
    login(req, res) {
        const data = req.body;
        const { username } = data;
        console.log(this);

        if (!this.userExists(username)) { //returns undefined
            res.status(404).json({ success: false, error: 'User not found' });
            return;
        }
        User.findOne(data, ('name', 'username'), (err, user) => {
            console.log(user);
            if (err) {
                res.status(400).json({ success: false, error: 'Error in sever' });
            } else {
                if (user) {
                    res.status(200).json({ success: true, user, token: user.generateToken() });
                } else {
                    res.status(404).json({ success: false, error: 'user not found' });
                }
            }
        });
    }

    create(req, res) {
        const data = req.body;
        if (this.userExists) {
            res.status(403).json({ success: false, error: 'User already exists' });
            return;
        }
        console.log(data);
        const user = new User(data);
        user.save((err, data) => {
            if (err) {
                console.log(err);
                res.status(400).json({ success: false, error: 'Error in server' });
            } else {
                res.status(200).json({ success: true, user });
            }
        });
    }
}

export default new userController();
Asim Dahal
  • 147
  • 2
  • 11
  • 2
    `userExists` doesn't return anything. The callback in the `findOne` function returns something but that's it. – Andy Oct 27 '18 at 15:50
  • Exactly, a return in inner function does not return to outer one. Would be better off using the `findOne()` promise instead assuming you are using a mongoose version that supports promises – charlietfl Oct 27 '18 at 15:51
  • If you add the code for `findOne` maybe we can think of a solution. – Andy Oct 27 '18 at 15:52
  • 1
    @Andy it is a mongoose method – charlietfl Oct 27 '18 at 15:53
  • The thing is I cannot access userExists method from the login method by the this.login method. I dont know why :/ – Asim Dahal Oct 27 '18 at 15:55
  • You are confusing accessing the method with the fact the method doesn't return anything. Also `findOne()` is **asynchronous** – charlietfl Oct 27 '18 at 15:56
  • Also, `userExists` needs a `name` passed to it. You're not doing that. – Andy Oct 27 '18 at 15:58
  • @andy passed the name, didnt work. Actually when I try to do `console.log(this)` it returns undefined. – Asim Dahal Oct 27 '18 at 16:01
  • [It should look something like this](https://jsfiddle.net/q4xo2mzj/1/) – Andy Oct 27 '18 at 16:02
  • @charlietfl ok even if findOne is asynchronous, shouldn't the userExists() function be accessible by `this.userExists()`? The thing is when I try to do this.userExists() is shows userExists is undefined – Asim Dahal Oct 27 '18 at 16:03
  • No it shows that the result of calling `this.userExists() ` is undefined not that `this.userExists` is undefined. Big difference. The latter would throw error. Again you are confusing accessing the method with what the method returns – charlietfl Oct 27 '18 at 16:28

0 Answers0