2015-08-04 19 views
5

Tôi làm việc kể từ repo này và cố gắng để chuyển đổi nó sang PostgreSQL đây là lỗi của tôi:Sequelize đối tượng không phải là một chức năng

/home/otis/Developer/Shipwrecked/Hatchway/node_modules/sequelize/lib/sequelize.js:601 
    this.importCache[path] = defineCall(this, DataTypes); 
          ^
TypeError: object is not a function 
    at Sequelize.import (/home/otis/Developer/Shipwrecked/Hatchway/node_modules/sequelize/lib/sequelize.js:601:30) 
    at db.sequelize (/home/otis/Developer/Shipwrecked/Hatchway/models/index.js:15:37) 
    at Array.forEach (native) 
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/models/index.js:14:6) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/app.js:10:12) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    at Object.<anonymous> (/home/otis/Developer/Shipwrecked/Hatchway/bin/www:7:11) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 

Dưới đây là đoạn code mà gây ra lỗi (mô hình/index.js):

var fs  = require("fs"); 
var path  = require("path"); 
var Sequelize = require("sequelize"); 
var env  = process.env.NODE_ENV || "development"; 
var config = require(__dirname + '/../config.json')[env]; 
var sequelize = new Sequelize(config.database, config.username, config.password, config); 
var db  = {}; 

fs 
    .readdirSync(__dirname) 
    .filter(function(file) { 
     return (file.indexOf(".") !== 0) && (file !== "index.js"); 
    }) 
    .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; 

Nếu tôi nhận xét ra các dòng sau lỗi dừng:

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

Node.js lỗi không phải là điểm mạnh của tôi vì vậy tôi không thực sự chắc chắn những gì đang xảy ra, trên tôi cũng có thể đăng config.json của tôi nếu đó là cần thiết?

+0

dán tệp 'models/index.js' của bạn –

+0

Đoạn giữa là index.js –

Trả lời

10

Nếu chúng ta nhìn vào code imports:

if (!this.importCache[path]) { 
    var defineCall = (arguments.length > 1 ? arguments[1] : require(path)); 
    this.importCache[path] = defineCall(this, DataTypes); 
    } 

... nó sẽ được hiểu rằng nó trông giống như một thư mục với các mô hình có một tập tin mà không phải là một mô hình tập tin hợp lệ và module thường chính xác. Theo đó, trong một nỗ lực để kích hoạt nó thông qua "mong muốn" có được lỗi. Kiểm tra các mô hình thư mục.

+1

Chúc mừng, đã tìm ra điều này ngay trước khi tôi thấy câu trả lời của bạn :) –

Các vấn đề liên quan