sequelize抛出错误方言需要在express js中显式定义

Posted

技术标签:

【中文标题】sequelize抛出错误方言需要在express js中显式定义【英文标题】:sequelize throwing error Dialect needs to be explicitly defined in express js 【发布时间】:2018-01-27 22:07:42 【问题描述】:

您好,我正在尝试使用 sequelize 查找所有查询。但它给出了错误:

错误:从 v4.0.0 开始需要明确提供方言 在新的 Sequelize (/home/europa/public_html/wired-rest-apis/node_modules/sequelize/lib/sequelize.js:170:13)

下面是我的 express js / node js 代码:-

  var Sequelize = require('sequelize');

    // db config
    var env = "dev";
    var config = require('./config.json')[env];
    var password = config.password ? config.password : null;

    // initialize database connection
    var sequelize = new Sequelize(
        config.database,
        config.user,
        config.password, 
            logging: console.log,
            define: 
                timestamps: false
            
        
    );

    app.get('/list_cars', function(req,res)

        const Image = sequelize.define('add_cars', /* ... */)
        const Car = sequelize.define('car_images', /* ... */)
        Car.hasMany(Image, as: 'Images', foreignKey:'add_car_id')
        Car.findAll(
         include: [
         model: Image
         ]
       )  .success(function(cars) 
            console.log(cars);
         );

    );

【问题讨论】:

【参考方案1】:

确保你在 config.json 中使用方言,


    "username" : "root",
    "password" : "root",
    "database" :"grijan",
    "host" : "localhost",
    "dialect" : "mysql",
    "name" : "add"

【讨论】:

以上是关于sequelize抛出错误方言需要在express js中显式定义的主要内容,如果未能解决你的问题,请参考以下文章

续集方言错误

Sequelize model.sync() 抛出错误

Grails 突然抛出错误无法确定数据库名称的 Hibernate 方言

按关联模型排序时,Sequelize 抛出错误“无法找到模型 x 的有效关联”

在 Express.js 中停止执行 Sequelize 承诺

Sequelize NodeJS 服务器抛出“ERR_UNKNOWN_ENCODING”错误