1

I've been trying to use pg-promise with passportjs, but I can't get to work local-signup. My login works well. Please see below:

    passport.use(
      "local-signup",
      new LocalStrategy(
        {
          usernameField: "email",
          passwordField: "pass",
          passReqToCallback: true
        },
        (req, email, pass, done) => {
          process.nextTick(function() {
            q.db
              .one("select email from users where email = $1", email)
              .then(data => {
                if (data) {
                  return done(
                    null,
                    false,
                    req.flash("signupMessage", "User already exists")
                  );
                } else {
                  console.log("here!?");
                  q.db
                    .none(
                      "insert into users (email, pass)" +
                        `values (${email}, ${pass})`
                    )
                    .then(data => {
                      console.log("created?");
                    });
                }
              })
              .catch(err => {
                console.log(err);
              });
          });
        }
      )
    );

The problem here is that, it actually detects if an user is already in the database, but if the user doesn't exists, it doesn't create the username, but instead, skips the whole process.

Any ideas how to fix this issue?

Thank you

8
  • Where does it fails? Is "here!?" message logged? Try to use second argument on none method for putting values to query Commented Jun 28, 2018 at 15:41
  • Also you concatenate query without any space between Commented Jun 28, 2018 at 15:42
  • No, "here" does not logs. It goes directly to the catch method: QueryResultError { code: queryResultErrorCode.noData message: "No data returned from the query." received: 0 query: "select email from users where email = '78'" } Commented Jun 28, 2018 at 15:43
  • From docs on one method: "When 0 or more than 1 rows are returned, the method rejects" Commented Jun 28, 2018 at 15:45
  • So try to use oneOrNone Commented Jun 28, 2018 at 15:46

1 Answer 1

2

Your code inside nextTick should be something like this:

q.db.task(async t => {
    const user = await t.oneOrNone('SELECT * FROM users WHERE email = $1', email);
    if (user) {
        return false;
    }
    await t.none('INSERT INTO users(email, pass) VALUES($1, $2)', [email, pass]);
    return true;
})
    .then(created => {
        done(
            null,
            false,
            req.flash('signupMessage', 'Created: ' + created)
        );
    })
    .catch(error => {
        console.log(error);
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.