Node Route:
exports.addUser = async ( req, res, next ) => {
const name = req.body.name;
const email = req.body.email;
const password = req.body.password;
const unique = await User.isUserUnique( email );
console.log( unique );
bcrypt.hash( password, 10, function ( err, hash ) {
const user = new User( name, email, hash );
//user.saveUser();
} );
res.send( req.body );
};
Model:
const client = require( "../database/index" );
class User {
constructor( name, email, password ) {
this.name = name;
this.email = email;
this.password = password;
}
static isUserUnique( email ) {
return client.connect( () => {
return client.db( "alliance" ).collection( "users" ).findOne( { email } ).then( ( user ) => {
return user;
} );
} );
}
Database:
const MongoClient = require( "mongodb" ).MongoClient;
const client = new MongoClient( url );
client.connect( function ( err ) {
if ( err ) {
console.log( err );
}
console.log( "Connected successfully to server" );
} );
module.exports = client;
Why is the unique constant that i declare in the Node Route undefined? Does it have something to do with the "return client.connect(())"? Basically I want the static method to return the user, so I assumed all you have to do inside of the Static method was return every value so they go up to the parent that called it, and then I would get it on the called constant. I have done something similair, except this time it is with connect method as the surrounding parent.