DEV Community

ilenterman
ilenterman

Posted on

Why is my promise not resolving correctly?

exports.addUser = async (req, res) => {
  const { username, email, password } = req.body;
  //hash password
  const password_hash = await hashPassword(password);
    //check whitelist
    this.checkWhitelist(email).then( function(response) {
      if (response) {
        console.log("RESOLVED TRUE")
        //POST user to Airtable
        new Promise(function(resolve, reject) {
          return usersTable.create(
            {
              email,
              username,
              password_hash,
              "email_verified":"false"
            },
            function(err) {
              if (err) {
                resolve(false);
                console.error(err);
                res.send({
                  "Success":false,
                  "responseCode":502,
                })
              } resolve(true);
              res.send({
                "Success":true,
                "responseCode":200,
              });
              }
            ).then( function(response) {
              if (response) {
                const EMAIL_SECRET = "xxxxxxxxxxx";
                jwt.sign(
                  {
                    'username': username,
                  },
                  EMAIL_SECRET,
                  {
                    expiresIn: '1d',
                  },
                  (err, emailToken) => {
                    const url = `http://localhost:3000/confirmation/${emailToken}`;

                    transporter.sendMail({
                      to: args.email,
                      subject: 'Confirm Email',
                      html: `Please click this email to confirm your email: <a href="${url}">${url}</a>`,
                    });
                  }
                )
              }
            })
      })} else {
        console.log('RESOLVED FALSE')
        res.send({
          "Success":false,
          "responseCode":403
        })
      }
    }
  )}

For some reason, the promise I created at usersTable.create is not resolving correctly. When I call .then() after, I get the error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of undefined.

For context, this is the user registration flow for a webapp. First, the pass is hashed, then the email is check against a whitelist (so far this logic is working). Now I just need to verify the email, but can't get the .then() to call correctly.-

What's going on?

Top comments (2)

Collapse
 
darquezt profile image
Damián Árquez • Edited

In your call to usersTable.create you are passing a callback function in the second argument and using the then. Basically you are handling the result twice. You have to choose whether you want to use a callback or a promise (then), but you can't use both.

Collapse
 
michi profile image
Michael Z • Edited

You are mixing async-await, then and callbacks together. I would suggest refactoring it all to only async-await for easier readability. Then your problem might solve itself automatically :)