CodexBloom - Programming Q&A Platform

Node.js application throws 'UnhandledPromiseRejectionWarning' when using Sequelize with PostgreSQL

πŸ‘€ Views: 1 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-10
node.js sequelize postgresql JavaScript

I'm relatively new to this, so bear with me. I need some guidance on I'm working with an scenario with my Node.js application that uses Sequelize to manage PostgreSQL database interactions... Specifically, when I run a query that attempts to fetch user details based on an ID, I receive an 'UnhandledPromiseRejectionWarning' with the message 'The query failed due to an invalid syntax'. I've double-checked my SQL syntax and it seems fine, and I'm handling the promise incorrectly. Here’s a snippet of my code: ```javascript const { Sequelize, DataTypes } = require('sequelize'); const sequelize = new Sequelize('postgres://user:pass@localhost:5432/mydb'); const User = sequelize.define('User', { username: DataTypes.STRING, email: DataTypes.STRING, }); async function getUserById(id) { try { const user = await User.findByPk(id); console.log(user); } catch (behavior) { console.behavior('behavior fetching user:', behavior); } } getUserById(1); ``` When I run this code, the warning appears, and I'm not sure why it's not being caught by the catch block since I'm already using async/await. I've tried adding a `process.on('unhandledRejection', (reason) => {...})` listener, but that doesn't seem to resolve the scenario. I also confirmed that the database connection works fine and that the Sequelize version is 6.6.2. Any advice on how to properly handle this or troubleshoot further would be appreciated! Has anyone dealt with something similar? I recently upgraded to Javascript 3.11. Is there a better approach? This is part of a larger REST API I'm building. Any ideas how to fix this?