NodeJS TypeError:sequelize.sync不是函数

ruarlubt  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(164)

帮助!当我运行'npm start'时,我一直得到这个错误

TypeError: sequelize.sync is not a function

下面是我的server.js的代码:

const express = require('express');
const routes = require('./routes')
// Import Sequelize Connection
const sequelize = require('sequelize');

const app = express();
const PORT = process.env.PORT || 3001;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(routes);

// Sync sequelize models to the database, then turn on the server
sequelize.sync({ force: true, alter: true }).then (() => {
  app.listen(PORT, () => 
    console.log(`App listening on port ${PORT}!`));
});

和/或

require('dotenv').config();

const Sequelize = require('sequelize');

const sequelize = process.env.JAWSDB_URL
  ? new Sequelize(process.env.JAWSDB_URL)
  : new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PW, {
      host: 'localhost',
      dialect: 'mysql',
      dialectOptions: {
        decimalNumbers: true,
      },
    });

module.exports = sequelize;

我所有的依赖都安装好了,不知道我还能做什么。我已经删除了我的package-lock.json和node模块,并做了缓存清理,但没有成功。
任何帮助感激不尽。谢谢!

olhwl3o2

olhwl3o21#

我想你错过了数据库连接部分,你可以找到infos here
您需要获取一个连接到实际数据库的sequelize示例,为此,您需要获取连接的url。有几个免费的postgresql数据库
您需要添加的内容如下:

const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname')
6jjcrrmo

6jjcrrmo2#

尝试像这样添加强制选项:

sequelize.sync({
    force: true
});

相关问题