我试图使用sequelize提供的parent.addChild()方法,但出于某种原因,它无法识别该函数并抛出错误。
我在这里读了很多其他的问题,他们都提到这些方法被添加到模型中。我阅读了整篇文档,重新阅读了the part,其中包含了我需要的信息,但只是我无法运行这个东西...我无法看到我在做什么这里错了,你们能帮帮忙吗?
// Models
const Person = Conn.define('person', {
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING,
allowNull: false
}
});
const Post = Conn.define('post', {
title: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.STRING,
allowNull: false
}
});
// Relationships
Person.belongsToMany(Post, {
as: 'person',
through: 'people_posts',
foreignKey: 'person_id'
});
Post.belongsToMany(Person, {
as: 'post',
through: 'people_posts',
foreignKey: 'post_id'
});
Conn.sync({ force: true }).then(() => {
const firstName = Faker.name.firstName();
const lastName = Faker.name.lastName();
return Person.create({
firstName: firstName,
lastName: lastName
})
.then(() => {
return Post.create({
title: `Title by ${firstName}`,
content: 'Content'
});
}).then(() => {
const person = Person.findOne({ where: { firstName: firstName }});
const post = Post.findOne({ where: { content: "Content" }})
return post.addPerson(person);
// this gives me: Unhandled rejection TypeError: post.addPerson is not a function
});
});