Next.js Discord

Discord Forum

Nodejs fullstack with PostgreSQL and session Store

Unanswered
European anchovy posted this in #help-forum
Open in Discord
European anchovyOP
Hello,

I am trying to deploy my project on vercel. I am using NodeJs and PostgreSQL. From what I can tell there might be an issue with the PostgreSQL session Store. I am using:

const session = require('express-session');
const PgSession = require('connect-pg-simple')(session);
require('dotenv').config();

const sessionStore = new PgSession({
  conString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DATABASE}`,
});

function createSessionConfig() {
  return {
    secret: 'super-secret',
    resave: false,
    saveUninitialized: false,
    store: sessionStore,
    cookie: {
      maxAge: 2 * 24 * 60 * 60 * 1000,
    },
  };
}

module.exports = createSessionConfig;


but I get an error and the deployment crashes with an internal server error:
Error: getaddrinfo ENOTFOUND undefined
    at /var/task/node_modules/pg-pool/index.js:45:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async PGStore._asyncQuery (/var/task/node_modules/connect-pg-simple/index.js:321:21) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'undefined'
}


I am using a middleware to store some cart information about the products in my locals from express response.locals. But it says that it is undefined.

const Cart = require('../models/cart.models');

function initializeCart(req, res, next) {
  let cart;

  if (!req.session.cart) {
    cart = new Cart();
  } else {
    const sessionCart = req.session.cart;
    cart = new Cart(
      req.session.cart.items,
      sessionCart.totalQuantity,
      sessionCart.totalPrice
    );
  }
  res.locals.cart = cart;

  next();
}

module.exports = initializeCart;

0 Replies