我正在使用sequelize.js V.5创建一个简单的api,用于发布书评。注释应该通过书号属于该书,并且与发布用户相同。
我不太确定如何创建它。
firebase.auth().onAuthStateChanged(user => {
console.log('user', user)
});
答案 0 :(得分:0)
根据文档,您需要关联对象
直接来自文档的标准示例
关联对象
Because Sequelize is doing a lot of magic, you have to call Sequelize.sync after setting the associations! Doing so will allow you the following:
Project.belongsToMany(Task)
Task.belongsToMany(Project)
Project.create()...
Task.create()...
Task.create()...
// save them... and then:
project.setTasks([task1, task2]).then(function() {
// saved!
})
// ok, now they are saved... how do I get them later on?
project.getTasks().then(function(associatedTasks) {
// associatedTasks is an array of tasks
})
// You can also pass filters to the getter method.
// They are equal to the options you can pass to a usual finder method.
project.getTasks({ where: 'id > 10' }).then(function(tasks) {
// tasks with an id greater than 10 :)
})
// You can also only retrieve certain fields of a associated object.
project.getTasks({attributes: ['title']}).then(function(tasks) {
// retrieve tasks with the attributes "title" and "id"
})
要删除创建的关联,您只需调用set方法即可,而无需指定特定ID:
// remove the association with task1
project.setTasks([task2]).then(function(associatedTasks) {
// you will get task2 only
})
// remove 'em all
project.setTasks([]).then(function(associatedTasks) {
// you will get an empty array
})
// or remove 'em more directly
project.removeTask(task1).then(function() {
// it's gone
})
// and add 'em again
project.addTask(task1).then(function() {
// it's back again
})
关于您的问题:
必须有类似的内容:
Comment.create(
{
// create new comment as you like
}
然后是类似的(根据需要)
comment.setUser(user).then(function() {
// saved!
})
comment.setBook(book).then(function() {
// saved!
})