# Console.log not showing for create script in Database Action Scripts

**URL:** <https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [March 14, 2024, 11:27pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737 "2024-03-14T23:27:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackthomas818](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackthomas818/32/55650_2.png) [@jackthomas818](https://community.auth0.com/u/jackthomas818)\
**Post date:** [March 14, 2024, 11:27pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/1 "2024-03-14T23:27:44Z")

</div>

I am unable to debug why my create script is not working. The only error message I get is “[Error] User not found” even if I set the callback to just return an error. No console logs appear when using the Real-time Webtask Logs extension. Also, the template code is completely outdated and not usable (for postgresql template).

Here is my create script:

```auto
function create(user, callback) {
  console.log('test!');
  const bcrypt = require('bcrypt');
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString:
      configuration.POSTGRES_URL,
  });

  bcrypt.hash(user.password, 10, (err, hashedPassword) => {
    if (err) {
      console.error(err);
      throw new Error("Something went badly wrong!");
    }

    const query =
      'INSERT INTO public."User"(email, password, "photoId") VALUES ($1, $2, 0) RETURNING email, password';

    pool.query(query, [user.email, hashedPassword], (err, res) => {
      if (err) {
        console.error(err);
        throw new Error("Something went badly wrong!");
      }

      console.log('User created:', res.rows[0]);
    });
  });
}

```

My login and get user scripts work without any issue:

```auto
async function login(email, password, callback) {
  const { Pool } = require('pg');
  const bcrypt = require('bcrypt');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL,
  });

  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('Wrong username or password'));
    }

    const user = rows[0];
    const isValid = await bcrypt.compare(password, user.password);

    if (!isValid) {
      return callback(new Error('Wrong username or password'));
    }

    return callback(null, {
      user_id: user.id,
      email: user.email,
    });
  } catch (err) {
    return callback(err);
  }
}

```

```auto
async function loginByEmail(email, callback) {
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL, // Make sure this is correctly set in your environment
  });
  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('User not found'));
    }

    const user = rows[0];
    return callback(null, {
      user_id: user.id,
      email: user.email,
      // You might want to return more fields depending on your requirements
    });
  } catch (err) {
    return callback(err);
  }
}

```

---

<div class="post-metadata">

**Author:** ![smolee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/smolee/32/60381_2.png) [@smolee](https://community.auth0.com/u/smolee)\
**Post date:** [September 12, 2024, 6:27pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/2 "2024-09-12T18:27:21Z")

</div>

Hi Jack,  
I am experiencing the same issue with console.log(). have you found a solution? it seems nobody has replied to your questions.

---

<div class="post-metadata">

**Author:** ![jackthomas818](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackthomas818/32/55650_2.png) [@jackthomas818](https://community.auth0.com/u/jackthomas818)\
**Post date:** [September 12, 2024, 6:48pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/3 "2024-09-12T18:48:08Z")

</div>

Hi Simon,

I was able to get this solved by going on a Zoom call with a dev from Auth0. Here are my working scripts, hopefully they will be useful. I can’t remember what actually fixed the issue but it was something simple that we missed.

Login Script:

```auto
async function login(email, password, callback) {
  console.log("using login script");
  const { Pool } = require('pg');
  const bcrypt = require('bcrypt');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL,
  });

  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('Wrong username or password'));
    }

    const user = rows[0];
    const isValid = await bcrypt.compare(password, user.password);

    if (!isValid) {
      return callback(new Error('Wrong username or password'));
    }

    return callback(null, {
      user_id: user.id,
      email: user.email,
    });
  } catch (err) {
    return callback(err);
  }
}

```

Create script:

```auto
function create(user, callback) {
  console.log("Using create script");
  const bcrypt = require('bcrypt');
  const { Pool } = require('pg');
  const pool = new Pool({
    connectionString:configuration.POSTGRES_URL
  });

  bcrypt.hash(user.password, 10, (err, hashedPassword) => {
    if (err) {
      return callback(new Error('Error hashing password'));
    }

    const query =
      'INSERT INTO public."User"(email, password, "photoId") VALUES ($1, $2, 0) RETURNING email, password';

    pool.query(query, [user.email, hashedPassword], (err, res) => {
      if (err) {
        if (err.code === '23505') {
          // Unique constraint violation
          return callback(new Error('User already exists'));
        }
        return callback(new Error('Error accessing database'));
      }

      if (res.rows.length > 0) {
        console.log('User created:', res.rows[0]);
        callback(null);
      } else {
        callback(new Error('User not created'));
      }
    });
  });
}

```

Get User Script:

```auto
async function loginByEmail(email, callback) {
  console.log("using get user script");
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL, // Make sure this is correctly set in your environment
  });
  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('User not found'));
    }

    const user = rows[0];
    return callback(null, {
      user_id: user.id,
      email: user.email,
      // You might want to return more fields depending on your requirements
    });
  } catch (err) {
    return callback(err);
  }
}

```

If you need more help I do recommend contacting Auth0 and hopping on a zoom call, they were very helpful.

Jack

---

<div class="post-metadata">

**Author:** ![jackthomas818](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackthomas818/32/55650_2.png) [@jackthomas818](https://community.auth0.com/u/jackthomas818)\
**Post date:** [September 12, 2024, 6:51pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/4 "2024-09-12T18:51:49Z")

</div>

After quickly looking at my create script, the difference was that you need to use the `callback` for a new error rather than just throwing an error.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 12, 2024, 9:28pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/5 "2024-09-12T21:28:34Z")

</div>

Thanks for sharing your solution @jackthomas818 ! Hopefully this helps you @smolee 🙏

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 26, 2024, 9:29pm UTC](https://community.auth0.com/t/console-log-not-showing-for-create-script-in-database-action-scripts/129737/6 "2024-09-26T21:29:09Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
