const testUser = await User.create({
name: "Divya Ranat",
email: "divyaranat@email.com"
});
const testBoard1 = await Board.create({
type: "Wood",
description: "Round",
rating: 7
});
const testBoard2 = await Board.create({
type: "Metal",
description: "Square",
rating: 9
});
await testUser.addBoards([testBoard1, testBoard2]);
const foundBoards = await testUser.getBoards();
const foundUser = await testBoard1.getUser();
console.log(foundUser)
我期待addBoards(), getBoards()和getUser()函数抛出错误。我不明白addBoards(), getBoards()和getUser()做了什么
代码:
const testUser = await User.create({
name: "Divya Ranat",
email: "divyaranat@email.com"
});
返回一个序列化实例给testUser。实例有数据和几个方法/函数。
与的例子:
await testUser.update({property: value})
你可以准确地更新数据库中的记录,而不是:
await User.update({property: value},{where: { id })
现在,如果您使用模型之间的关联,sequelize将函数添加到实例中,如addBoards。
如果传递错误的数据,它将抛出错误。