Open MDoughlin opened 1 year ago
My Promise.all block is what is throwing the error
remove comment from .env file and then it will work
I would love a second set of eyes on this please..
This is my code to register
export async function register(req,res){
try { const { username, password, profile, email } = req.body; // check the existing user const existUsername = new Promise((resolve, reject) => { UserModel.findOne({ username }, function(err, user){ if(err) reject(new Error(err)) if(user) reject({ error : "Please use unique username"}); resolve(); }) }); // check for existing email const existEmail = new Promise((resolve, reject) => { UserModel.findOne({ email }, function(err, email){ if(err) reject(new Error(err)) if(email) reject({ error : "Please use unique Email"}); resolve(); }) }); Promise.all([existUsername, existEmail]) .then(() => { if(password){ bcrypt.hash(password, 10) .then( hashedPassword => { const user = new UserModel({ username, password: hashedPassword, profile: profile || '', email }); // return save result as a response user.save() .then(result => res.status(201).send({ msg: "User Register Successfully"})) .catch(error => res.status(500).send({error})) }).catch(error => { return res.status(500).send({ error : "Enable to hashed password" }) }) } }).catch(error => { return res.status(500).send({ error }) }) } catch (error) { return res.status(500).send(error); }
}
In my Thunderbolt I am getting a 500 error with
{ "error": {} }
What I have done so far:
* I have retyped the code * Compared what I had to code provided on Github * Even though it is throwing an error I provided an different username, password and email even though I know example is not in the database
const existUsername = new Promise((resolve, reject) => {
UserModel.findOne({ username }, function (err, user) {
if (err) reject(new Error(err));
if (user) reject({ error: "Please use unique username" });
resolve();
});
});
Instead handle this with the code below by using the async await method
const existUsername = await UserModel.findOne({
username,
function(err, user) {
if (err) reject(new Error(err));
if (user) reject({ error: "Please use unique username" });
},
});
const existEmail = await UserModel.findOne({
email,
function(err, email) {
if (err) reject(new Error(err));
if (email) reject({ error: "Please use unique username" });
},
});
Hopefully this works for you. Thank me later
@nkayilovette03 still getting the same error
I would love a second set of eyes on this please..
This is my code to register
export async function register(req,res){
}
In my Thunderbolt I am getting a 500 error with
{ "error": {} }
What I have done so far: