@oprk21fjin3

"Dialect needs to be explicitly supplied as of v4.0.0?

При запуске npm run dev возникает следующая ошибка
throw new Error("Dialect needs to be explicitly supplied as of v4.0.0");
index.js
require('dotenv').config
const express =  require('express')
const sequelize = require('./db')
const models = require('./models/models')
const PORT = process.env.PORT || 5000
const app = express()
process.env.NODE_ENV = 'production';



const start = async () => {
    try{
        sequelize.authenticate()
        await sequelize.sync()
    app.listen(PORT, () => console.log('Server started on port ${PORT}'))
}   catch(e) {
    console.log(e)
}
}


model.js
const sequelize = require('./db')
const {DataTypes} = require

const User = sequelize.define('user',
 { id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
 email: {type: DataTypes.STRING, unique: true,},
 password: {type: DataTypes.STRING},
 role: {type: DataTypes.STRING, defaultValue: "USER"}

})

const Basket = sequelize.define('basket',
 { id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
})

const BasketDevice = sequelize.define('basket_device',
 { id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
})

const Device = sequelize.define('device',{
 id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
 name: {type: DataTypes.STRING, unique: true, allowNull: false},
 price: {type: DataTypes.STRING, allowNull: false},
 raiting: {type: DataTypes.STRING, defaultValue: 0},
 img: {type: DataTypes.STRING, allowNull: false},
})

const Type = sequelize.define('type',{ 
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
 name: {type: DataTypes.STRING, unique: true, allowNull: false},
})

const Brand = sequelize.define('brand',{ 
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
})

const Rating = sequelize.define('rating',{ 
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    rate: {type: DataTypes.INTEGER, allowNull: false},
   
})

const DeviceInfo = sequelize.define('device_info',{ 
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    title: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    description: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
})

const TypeBrand = sequelize.define('type_brand', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
})

User.hasOne(Basket) // один к одному (hasone 1 к 1) (hasMany там куда смотрит стрелка )
Basket.belongsTo(User) //Корзина принадлежит пользователю

User.hasMany(Raiting) //Пользователь может иметь неск. оценок
Raiting.belongsTo(User)

Basket.hasMany(BasketDevice)
BasketDevice.belongsTo(Basket)

Type.hasMany(Device)
Device.belongsTo(Type)

Brand.hasMany(Device)
Device.belongsTo(Brand)

Device.hasMany(Raiting)
Raiting.belongsTo(Device)

Device.hasMany(BasketDevice)
BasketDevice.belongsTo(Device)

Device.hasMany(DeviceInfo);
DeviceInfo.belongsTo(Device)

Type.belongsToMany(Brand, {through})
Brand.belongsToMany(Type, {through})


module.exports = {
    User,
    Basket,
    BasketDevice,
    Device,
    Type,
    Brand,
    Rating,
    
}


.env
PORT = 5432
DB_NAME = online_store
DB_USER = postgres
DB_PASSWORD = 2015
DB_HOST = localhost
DB_PORT = 5432


package.json
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js"  
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.21.3"
  }
}


db.js

const {Sequelize} = require('sequelize')

module.exports = new Sequelize (
    process.env.DB_NAME, //НАЗВАНИЕ БД
    process.env.DB_USER, //Пользователь
    process.env.DB_PASSWORD, //Пароль
    process.env.DB_NAME,
    {
        dialect: 'postgres',
        host: process.env.DB_HOST,
        port: process.env.DB_PORT, 
    }
)


Как решить данную проблему?
  • Вопрос задан
  • 38 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы