akashyap2013 / MERN_Login_App_with_ResetEmail

In this project, we are going to create the MERN Login App with Reset Email Feature.
234 stars 162 forks source link

Can not register #8

Open MDoughlin opened 1 year ago

MDoughlin commented 1 year ago

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:

Screenshot 2023-06-05 at 3 33 54 PM
MDoughlin commented 1 year ago

My Promise.all block is what is throwing the error

adivya-jain commented 1 year ago

remove comment from .env file and then it will work

nkayilovette03 commented 6 months ago

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
Screenshot 2023-06-05 at 3 33 54 PM

TRY THIS INSTEAD FAM

Instead of doing this as in the tutorials;

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

MDoughlin commented 6 months ago

@nkayilovette03 still getting the same error