你好,我是nodejs的新手,目前我将Sequelize与NodeJs集成在一起。我制作了不同的模型并定义了关联,但是当我获取数据时,我收到了以下错误消息
TypeError:include.model.getTableName不是函数
我的盒子模型
const Sequelize = require('sequelize');
const PostBox = require("./PostBox");
module.exports = function (sequelize) {
var Box = sequelize.define('box', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
references: {
model: PostBox,
key: "box_id" // this is the `posts.post_id` column - name it according to your posts table definition (the one defined in ./Post)
}
}
}, {timestamp: false});
Box.associate = function (models) {
Box.hasMany(models.PostBox, {targetKey: 'user_posts_id',foreginkey: 'id'});
};
return Box;
};
邮箱模型
const Sequelize = require('sequelize');
var Post = require('./Post');
module.exports = function (sequelize) {
var PostBox = sequelize.define('user_posts_boxes', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
user_posts_id: {
type: Sequelize.INTEGER,
references: {
model: Post,
key: "id" // this is the `posts.post_id` column - name it according to your posts table definition (the one defined in ./Post)
}
},
box_id: {
type: Sequelize.INTEGER,
},
user_id: {
type: Sequelize.INTEGER
},
post_type_id: {
type: Sequelize.INTEGER
}
}, {timestamp: false}
);
PostBox.associate = function (models) {
PostBox.belongsTo(models.Post, {targetKey: 'id', foreignKey: 'user_posts_id'});
};
return PostBox;
};
这是我从中调用关联模型的基本模型
const Sequelize = require('sequelize');
var Box = require('./Box');
const sequelize = new Sequelize('store_db', 'root', 'root', {
host: 'localhost',
dialect: 'mysql'
});
const User = require("./User")(sequelize);
const PostBox = require("./PostBox")(sequelize);
const Post = require("./Post")(sequelize);
function findOneUer() {
return User.findOne({attributes: ['id', 'username']});
}
function findPostById(id) {
var post = PostBox.findOne({
attributes: ['id', "user_posts_id", "box_id"],
where: {id: id},
include: [
{model: Box, required: true}
]
});
return post;
}
module.exports = {findPostById: findPostById};
当我从基本模式调用 findPostById(2)方法时,我收到了此错误。