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:
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:
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);
}
}
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);
}
}