Search code examples
javascriptnode.jsexpresssequelize.js

SCRAM-SERVER-FIRST-MESSAGE: client password must be a string


Ive read documentation from several pages on SO of this issue, but i havent been able to fix my issue with this particular error.

    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')
    ^

Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
    at Object.continueSession (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\sasl.js:24:11)
    at Client._handleAuthSASLContinue (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\client.js:257:10)
    at Connection.emit (events.js:400:28)
    at C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\connection.js:114:12
    at Parser.parse (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg-protocol\dist\parser.js:40:17)
    at Socket.<anonymous> (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)

its as if in my connectDB() function its not recognizing the password to the database. I am trying to run a seeder.js script to seed the database with useful information for testing purposes, and if i run npm run server which is a script that just starts a nodemon server, itll connect to the DB just fine. but when i try to run my script to seed data, i am returning this error.

import { Sequelize } from "sequelize";
import colors from "colors";
import dotenv from "dotenv";

dotenv.config();

const user = "postgres";
const host = "localhost";
const database = "thePantry";
const port = "5432";

const connectDB = async () => {
  const sequelize = new Sequelize(database, user, process.env.DBPASS, {
    host,
    port,
    dialect: "postgres",
    logging: false,
  });
  try {
    await sequelize.authenticate();
    console.log("Connection has been established successfully.".bgGreen.black);
  } catch (error) {
    console.error("Unable to connect to the database:".bgRed.black, error);
  }
};

export default connectDB;

above is my connectDB() file, and again, it works when i run the server normally. but i receive this error only when trying to seed the database. Ill post my seeder script below:

import dotenv from "dotenv";
import colors from "colors";
import users from "./data/users.js";

import User from "./models/userModel.js";

import connectDB from "./config/db.js";

dotenv.config();
console.log(process.env.DBPASS);

connectDB();

const importData = async () => {
  try {
    await User.drop();
    await User.sync();

    await User.bulkCreate(users);

    console.log("Data Imported".green.inverse);
    process.exit();
  } catch (e) {
    console.error(`${e}`.red.inverse);
    process.exit(1);
  }
};

const destroyData = async () => {
  try {
    await User.bulkDestroy();

    console.log("Data Destroyed".red.inverse);
    process.exit();
  } catch (e) {
    console.error(`${e}`.red.inverse);
    process.exit(1);
  }
};

if (process.argv[2] === "-d") {
  destroyData();
} else {
  importData();
}


Solution

  • So, i may have figured this out by playing around in another project with sequelize, as it turns out, the initial connection to the database in my server.js file, honestly means nothing. Unlike Mongoose where the connection is available across the whole app. its not the same for Sequelize this connection that it creates is only apparent in certain places, for example i was trying the same process in my other project as i am here, except i was trying to read data from my DB using the model that i built with sequelize and i was receiving the same type error, i went into where i defined the model and made a sequelize connection there, and i was then able to read from the database using that object model.

    Long story short, to fix the error in this app i have to place a connection to the database in the seeder.js file or i have to place a connection in the User model (this is ideal since ill be using the model in various places) to be able to seed information or read information from the database.