Tuesday, 30 January 2018

Controller "ignoring" method

Hello, there! I've been coding a method to create new users in my API, but I have to verify if that user exists, before creating it. I've coded a method to verify it, but it's not returning true or false as I expected...Usercontroller.jsconst Promise = require('bluebird') const UserModel = require('../models/UserModel')() class UserController { constructor() { this.User = Promise.promisifyAll(UserModel) } authenticate(req, res, next) { let json = req.body this.User.findAsync({ email: json.email, password: json.password }).then(data => { if (data.length === 0) { res.status(404) res.json({ status: 'failed', message: 'Ops, it seems this user is not registered!' }) } res.json({ status: 'success', data: data[0].email }) }).catch(next) } create(req, res, next) { let json = req.body if (this.alreadyExists(json.email, next) === true) { res.status(400) res.json({ status: 'failed', message: 'Ops, this user already exists!' }) } this.User.createAsync(json) .then((err, data) => { res.json({ status: 'success', message: 'Yeah, user registered successfully' }) }) .catch(next) } alreadyExists(email, next) { return this.User.findAsync({ email: email}) .then(data => { if (data.length >= 0) { return true } return false }) .catch(next) } } module.exports = new UserController()

Submitted January 30, 2018 at 12:35PM by giocruz

No comments:

Post a Comment