Sequelize.define() 不是函数吗? [英] Sequelize.define() is not a function?

查看:18
本文介绍了Sequelize.define() 不是函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 node.js 网络服务器中使用 Sequelize.

Trying to play around with Sequelize in a node.js webserver.

我已经像这样在 index.js 中初始化了 Sequelize 连接池

I have initialised the Sequelize connection pool in index.js like so

index.js

const config = require('./config/config');
const app = require('./config/express');
const Sequelize = require('sequelize');

const sequelize = new Sequelize(config.mysql.database, config.mysql.user, config.mysql.pass, {
  host: config.mysql.host,
  dialect: 'mysql',
  operatorsAliases: false,

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.'); // eslint-disable-line no-console
  })
  .catch((err) => {
    console.error('Unable to connect to the database:', err); // eslint-disable-line no-console
  });

module.exports = { app, sequelize };

user.js 模型

const sequelize = require('sequelize');
const DataTypes = require('mysql');
/**
 * User model
 */
const User = sequelize.define('user', {

  id: {
    type: DataTypes.INTEGER,
  },
  name: {
    type: DataTypes.STRING,
  },
  email: {
    type: DataTypes.STRING,
  },
  lastEmail: {
    type: DataTypes.TIME,
  }
});

尝试启动服务器时出现以下错误

When trying to start the server I get the following error

TypeError: sequelize.define is not a function ... user.js

TypeError: sequelize.define is not a function ... user.js

我猜 sequelize 对象没有被全局化,但是我在创建模型之前测试了连接,它很好.

I am guessing the sequelize object is not being made global, however I tested the connection before creating the model and it was fine.

推荐答案

在 user.js 中,您尝试在不是实例的东西上使用实例方法 .define().在 index.js 中,您确实通过调用构造函数来创建实例,但这在 user.js 中不可用.这里的解决方案实际上取决于您要完成的工作以及您希望如何组织代码.通常 index.js 是您的应用程序的入口点,因此从那里导出 appsequelize 有点不寻常,但如果这是您想要做的,然后在您的 user.js 文件中,您可能需要 index.js,并使用您在此处定义的 sequelize 实例.在这种情况下,您的 index.js 将保持不变,而 User.js 可能只是在第一个 require 中有更改:

In user.js you are trying to use an instance method .define() on something that is not an instance. In index.js you do create an instance by calling the constructor, but that is not available in user.js. The solution here really depends on what you are trying to accomplish and how you want to organize your code. Normally index.js is the entry point to your application, so it is a little unusual to be exporting app and sequelize from there, but if that is what you want to do, then in your user.js file you could require index.js, and use the sequelize instance that you defined there. In that case, your index.js would stay the same and User.js might just have a change in the first require:

const sequelize = require('index.js').sequelize;
const DataTypes = require('mysql');

/**
 * User model
 */
const User = sequelize.define('user', {

  id: {
    type: DataTypes.INTEGER,
  },
  name: {
    type: DataTypes.STRING,
  },
  email: {
    type: DataTypes.STRING,
  },
  lastEmail: {
    type: DataTypes.TIME,
  }
});

我不一定推荐这种方法,因为它确实取决于您想要做什么(我想您可能会遗漏一些代码?),但它确实显示了一种使用从 user.js 中的 index.js 实例化 sequelize 对象

I'm not necessarily recommending this approach, because, again, it really depends on what you are trying to do (I think maybe you are leaving out some of your code?), but it does show one way to use the instantiated sequelize object from index.js in user.js

这篇关于Sequelize.define() 不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆