javascript - Sequelize.define报错
巴扎黑
巴扎黑 2017-04-11 11:23:09
[JavaScript讨论组]
TypeError: Sequelize.define is not a function

搭建了一个 koa的项目,使用Sequelize操作数据库;
app.js里面引用
const Sequelize = require('sequelize');
const sqlconfig=require('./config');
config文件是数据库的信息;
然后在app.js里面创建一个sequelize对象实例:

new Sequelize(sqlconfig.database, sqlconfig.username, sqlconfig.password, {
  host: sqlconfig.host,
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    idle: 30000
  }
});

在项目里面建了一个model层,新建文件feedBack.js,定义模型,内容如下:

var sequelize = require('sequelize');

var FeedBack = sequelize.define('feedBackInfo', {
    id:{
        type: sequelize.BIGINT(19),
        primaryKey: true

    },
    third_id: {
        type: sequelize.STRING(50)
    },
    apply_no: {
        type: sequelize.STRING(20)
    },
    description: {
        type: sequelize.Text
    },
    page_source: {
        type: sequelize.STRING(20)
    },
    create_time: {
        type: sequelize.DATETIME
    },
    update_time: {
        type: sequelize.DATETIME
    }

}, {
    freezeTableName: true, // Model 对应的表名将与model名相同
    timestamps: false
});
module.exports = FeedBack;

在routes文件里面引入文件:
var FeedBack=require('../model/userFeedBackinfo');
此时报错啦:

    /Users/iqianjin-zhangshanshan/koa2-Demo/model/userFeedBackInfo.js:5
var FeedBack = sequelize.define('feedBackInfo', {
                         ^

TypeError: sequelize.define is not a function
    at Object. (/Users/iqianjin-zhangshanshan/koa2-Demo/model/userFeedBackInfo.js:3:26)
    at Module._compile (module.js:541:32)
    at loader (/Users/iqianjin-zhangshanshan/koa2-Demo/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/iqianjin-zhangshanshan/koa2-Demo/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object. (/Users/iqianjin-zhangshanshan/koa2-Demo/routes/index.js:3:14)
    at Module._compile (module.js:541:32)
    at loader (/Users/iqianjin-zhangshanshan/koa2-Demo/node_modules/babel-register/[nodemon] app crashed - waiting for file changes before starting...
请各位大神帮忙解决一下,我找了很多资料都没有找到是怎么回事
巴扎黑
巴扎黑

全部回复(1)
迷茫
  • define方法是在sequelize实例上才有的,不是在全局上的

const sequelizeInstance = new Sequelize(sqlconfig.database, sqlconfig.username, sqlconfig.password, {
  host: sqlconfig.host,
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    idle: 30000
  }
});
var FeedBack = sequelizeInstance.define('feedBackInfo', {
    id:{
    .....
    
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号