0

I'm working on creating a table of users using Bcrypt and keep getting an error:

(node:54133) UnhandledPromiseRejectionWarning: Error: Client was closed and is not queryable

I'm recycling some code from a previous project that is working on that project and not sure where I'm going wrong with it. Below is my test function sitting in my seed.js as well as the function it's calling. As far as I'm seeing it's hitting the helper function but then erroring out, when I console.log my fields in the helper function, I'm getting all the fields, but it's not inserting into my Postgres table.

Seed function

async function testUsers() {
  try {
    console.log("testing");
    bcrypt.hash("bertie99", SALT_COUNT, async function (err, hashedPassword) {
      console.log("71", err);
      const starter = await createUser({
        username: "userone",
        password: hashedPassword,
        email: "123@yahoo.com",
      });
      console.log(starter);
    });
  } catch (error) {
    console.log(error);
  }
}

Helper function

async function createUser({ username, password, email }) {
  try {
    console.log("email", email);
    console.log("username", username);
    console.log("password", password);
    const result = await client.query(
      `
      INSERT INTO users(username, password, email)
      VALUES ($1, $2, $3);
    `,
      [username, password, email]
    );

    return result;
  } catch (error) {
    throw error;
  }
}
Xichi
  • 61
  • 7
  • If you try to query something simple, without the hash it works? – Talg123 Jul 12 '20 at 16:58
  • If I'd run something like the below, it returns and inserts the user into the database, however my console.logs inside create user shows what it should be inserting, my table has all been set to varchar. ``` const starter = await createUser({ username: "userone", password: "123456", email: "test1234", }); – Xichi Jul 12 '20 at 19:27

1 Answers1

0

So, it appears that the hashing need an await on it, I edited the function as below and it is working.

  try {
    console.log("testing");
    const hash = await bcrypt.hashSync("bertie99", saltRounds);
    const starter = await createUser({
      username: "userone",
      password: hash,
      email: "123@yahoo.com",
    });
    console.log(starter);
  } catch (error) {
    console.log(error);
  }
}
Xichi
  • 61
  • 7