Search code examples
node.jsdockerdocker-composeserverlessserverless-framework

AssertionError [ERR_ASSERTION]: unknown message code: 4a (Serverless - MySQL - NodeJS)


I am getting following error when creating a connection to MySQL.

POST /signup (λ: signup)
✖ Uncaught exception
Environment: darwin, node 20.0.0, framework 3.30.1 (local), plugin 6.2.3, SDK 4.3.2
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
AssertionError [ERR_ASSERTION]: unknown message code: 4a
    at Parser.handlePacket (/Volumes/Work/Learning/CWJ MicroServices/cwj-serverless-nodejs/user-service/node_modules/pg-protocol/dist/parser.js:140:34)
    at Parser.parse (/Volumes/Work/Learning/CWJ MicroServices/cwj-serverless-nodejs/user-service/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/Volumes/Work/Learning/CWJ MicroServices/cwj-serverless-nodejs/user-service/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:511:28)
    at addChunk (node:internal/streams/readable:332:12)
    at readableAddChunk (node:internal/streams/readable:305:9)
    at Readable.push (node:internal/streams/readable:242:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
libc++abi: terminating due to uncaught exception of type Napi::Error
make: *** [server] Abort trap: 6

I debugged and found this line giving the error.

UserRespository.js

import { UserModel } from "../models/UserModel";
import { DBClient } from "../utility/databaseClient";
export class UserRespository {
  constructor() {}

  async createAccount({ email, password, salt, phone, userType }: UserModel) {
    //Following lines giving the error.
    const client = await DBClient();
    await client.connect();
  }
}

databaseClient.js

import { Client } from "pg";

export const DBClient = () => {
  return new Client({
    host: "127.0.0.1",
    user: "root",
    database: "user_service",
    password: "root",
    port: 3306,
  });
};

docker-compose.yml

version: "3.9"
services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_PASSWORD=root
      - MYSQL_DATABASE=user_service
    ports:
      - "3306:3306"
    volumes:
      - db:/var/lib/mysql

volumes:
  db:
    driver: local

enter image description here

Docker is running, and when I try to client.connect() it is giving me the above error. I created docker-compose file and now it is running on the docker, and I am using Table Plus client and it is doing fine too.


Solution

  • It was due to my neglect, PG- stands for postgress, and now I removed that and use the mysql.