Danfeng
Danfeng

Reputation: 1033

Unhandled rejection SequelizeDatabaseError: No database selected

I'm using sequelize in my Express.js application with MySQL.

All database credentials are stored in .env file and are correct:

DATABASE_NAME= mydb
DATABASE_USERNAME= root
DATABASE_PASSWORD= pass
DATABASE_HOST= localhost
DATABASE_PORT= 3306

database.js file:

require('dotenv').config();
const Sequelize = require('sequelize');

module.exports = new Sequelize(
    process.env.DATABAST_NAME,
    process.env.DATABASE_USERNAME,
    process.env.DATABASE_PASSWORD,
    {
        port: process.env.DATABASE_PORT,
        host: process.env.DATABASE_HOST,
        dialect: 'mysql'
    }
);

In user model:

    const Sequelize = require('sequelize');
    const db = require('../config/database');

    module.exports = db.define('user', {
        username: {
            type: Sequelize.STRING,
            unique: true,
            primaryKey: true
        },
        email: {
            type: Sequelize.STRING,
            unique: true
        },
        password: {
            type: Sequelize.STRING
        },
    }, {
        freezeTableName: true,
        timestamps: false
    });

  user.findAll().then(data=>{
        console.log(data)
   });

Database 'mydb' has only one table 'user', with the application start, I got an error:

Executing (default): SELECT `username`, `email`, `password` FROM `user` AS `user`;
Unhandled rejection SequelizeDatabaseError: No database selected
    at Query.formatError (C:\Users\me\WebstormProjects\Backend\node_modules\sequelize\lib\dialects\mysql\query.js:241:16)
    at Query.handler [as onResult] (C:\Users\me\WebstormProjects\Backend\node_modules\sequelize\lib\dialects\mysql\query.js:48:23)
    at Query.execute (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\commands\command.js:30:14)
    at Connection.handlePacket (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:449:32)
    at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:72:12)
    at PacketParser.executeStart (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.Connection.stream.on.data (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:79:25)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  1. I tried to test .env file with below code, and everything is readable:

    var config = require('dotenv').config(); console.log(config)

  2. I tried to add database name in front of 'user', in db.defind(), and i got the same issue.

Please, if you have any suggestions, let me know.

Upvotes: 0

Views: 5429

Answers (2)

Ryan Shillington
Ryan Shillington

Reputation: 25088

Your database name is empty when you're initializing Sequelize. It looks like you spelled DATABASE wrong.

module.exports = new Sequelize(
    process.env.DATABAST_NAME, // <--- This is an empty string or null.
    process.env.DATABASE_USERNAME,
    process.env.DATABASE_PASSWORD,
    {
        port: process.env.DATABASE_PORT,
        host: process.env.DATABASE_HOST,
        dialect: 'mysql'
    }
);

Upvotes: 0

Aziz Ahmed
Aziz Ahmed

Reputation: 1

  1. Install dotenv package from npm with command:
npm install dotenv
  1. Require dotenv in your main.ts file which is located inside the 'src' folder by adding the below lines:
import * as dotenv from "dotenv";
dotenv.config();
  1. Run your project once again.

Upvotes: -1

Related Questions