我有一个问题
有一个模型文件..
模型/ user.js的
module.exports = {
get: function () {
//configuration database
var database=require(""+appDir+"/config/database");
//sequelize modul
var Sequelize=require("sequelize");
//set connection
var connection = new Sequelize(database.database,database.user,database.password, {
host: database.host,
dialect: database.dialect,
pool: {
max: 5,
min: 0,
idle: 10000
}
});
//table definition
var User = connection.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
status: {
type: Sequelize.INTEGER
}
});
//table definition
var Tasks = connection.define('tasks', {
taskname: {
type: Sequelize.STRING
},
user_id: {
type: Sequelize.INTEGER
}
});
User.hasMany(Tasks, {foreignKey: 'user_id'});
Tasks.belongsTo(User, {foreignKey: 'user_id'});
return User;
}
};
使用以下代码; 使用require调用模型文件 此代码不包含属性..但不适用于包含属性
//model call
var model=require("../model/user.js");
model.sync().then(function()
{
model.find({include: [Task]}).then(function(user)
{
callback(user);
})
.catch(function(error)
{
callback(error);
})
});
但它不起作用..我想加入两个表..
答案 0 :(得分:0)
您只导出model / user.js中的get函数,但之后您尝试调用未定义的同步函数。首先,尝试获取用户模型(Sequelize模型),然后调用它的同步功能:
var model = require("../model/user.js");
model.get().sync()