簡體   English   中英

Sequelize 錯誤:錯誤:從 v4.0.0 開始需要明確提供方言

[英]Sequelize Error: Error: Dialect needs to be explicitly supplied as of v4.0.0

在准備部署應用程序時,出現錯誤:

 Error: Dialect needs to be explicitly supplied as of v4.0.0

每當我運行 npm run develop 時就開始彈出。 我正在為我的環境變量使用 a.env 文件。 connection.js 代碼是:

const Sequelize = require('sequelize');
require('dotenv').config({path: '../.env'});

let sequelize;

if (process.env.JAWSDB_URL) {
  sequelize = new Sequelize(process.env.JAWSDB_URL);
} else {
  sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USER,
    process.env.DB_PASSWORD,
    {
      dialect: 'mysql',
      host: 'localhost',
      port: 3306
    }
  );
} 

module.exports = sequelize;
 The server.js file is:
const path = require('path');
const express = require("express");
const cors = require("cors");
const routes = require("./routes");
const sequelize = require("./config/connection");

const app = express();

const PORT = process.env.PORT || 8080;
var corsOptions = {
  //for online use
  // origin: "https://operations-limit-database.herokuapp.com"
  //first trying this without credentials to see how it works
  // credentials: true,

  //for local use
  origin: "http://localhost:3000"
};
app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

// turn on routes
app.use(routes);
// simple route


//the below two are for deployed builds
if (process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, '../client/build')));
}

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '../client/build/index.html'));
});

//this is for local build only
// app.get("/", (req, res) => {
//   res.json({ message: "Welcome to Homeschool's Op Limit Database" });
// });

// turn on connection to db and server
//{ force: false } to drop tables and recreate
sequelize.sync().then(() => {
  app.listen(PORT, () => console.log(`Server is running on port ${PORT}.`));
});
 And the .env file looks like below:
DB_NAME= 'xxxx'
DB_USER= 'yyyy'
DB_PASSWORD= 'zzzz'
DB_SECRET='aaaa'

不確定我錯過了什么,它在周末啟動並運行然后就停止了。 我可以嘗試的任何事情都會受到贊賞。

到目前為止,我已經嘗試重新排序 the.env、更改數據庫和重建應用程序,但都無濟於事。

當您沒有將方言正確傳遞給 sequelize 時會發生這種情況,請確保您傳遞的是字符串並且環境變量正在傳遞,通常是這種情況。

 const sequelize = new Sequelize('database', 'username', 'password', { dialect: 'mysql', dialectOptions: { // Your mysql2 options here } })

文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM