续集未定义

时间:2016-11-04 20:07:23

标签: mysql node.js sequelize.js

我正在为我的orm数据库使用sequelize。我使用的是mysql。

运行项目时出现此错误。

var accounts = sequelize.define('OkAkun' , {//attrbut 
                         ^

ReferenceError: sequelize is not defined
    at module.exports (/path_to_project/Backend/app/models/OkAkun.js:7:26)
    at Sequelize.import (/path_to_project/Backend/node_modules/sequelize/lib/sequelize.js:691:30)
    at /path_to_project/Backend/app/models/index.js:30:32
    at Array.forEach (native)
    at Object.<anonymous> (/path_to_project/Backend/app/models/index.js:29:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/path_to_project/Backend/app/routes/routers.js:4:13)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)

查看我的文件

  

index.js

"use strict";
var fs = require('fs');
var path = require ('path');
var Sequelize = require ('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || "development";
var dbConfig =require(__base + "config/initializers/config.js").database;
var sequelize;

sequelize = new  Sequelize(dbConfig[env].database , dbConfig[env].username , dbConfig[env].password,
            {
                "dialect": "mysql",
                "host"  :dbConfig[env].host,
                "port"  :dbConfig[env].port,
                "logging": false,
                "define": {underscored: true}
            });

var db = {};

fs
.readdirSync(__dirname)
.filter(function(file){
    return (file.indexOf(".") !== 0) && (file !== basename);
})
 .forEach(function(file) {
    var model  = sequelize.import(path.join(__dirname,file));
    db[model.name] = model;
 });

Object.keys(db).forEach(function (modelName){
    if("associate" in db[modelName]){
        db[modelName].associate(db);
    }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;



module.exports = db;

这是我的模特。当我运行项目时。它显示了未定义的续集错误。

  

OkAkun.js

module.exports  = function () {

var accounts = sequelize.define('OkAkun' , {//attrbut 

    id_akun : {
        autoIncrement: true,
        type : DataTypes.INTEGER,
        allowNull: false,
        primaryKey : true
    },
    username:{
        type : DataTypes.STRING,
        allowNull: false
    },
    phone_number:{
        type: DataTypes.STRING,
        allowNull:false
    },
    email : {
        type: DataTypes.STRING,
        allowNull: true
    },
    create_at : {
        type: DataTypes.DATE,
        allowNull: false    
    },
    update_at : {
        type : DataTypes.DATE,
        allowNull: false
    },
    password : {
        type: DataTypes.STRING,
        allowNull: false
    }
});

return accounts;

};

Fyi,我正在使用orm作为数据库,如sequelize。而且我不明白我在这里失去了什么步骤。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要将sequelizeDataTypes添加到模型函数的参数中。当你sequelize.import时,Sequelize会传递这些内容。

另请参阅https://github.com/sequelize/express-example/blob/master/models/task.js了解工作示例