如何在 bookshelf.js 上一次保存多个寄存器
How to save more than one register at once on bookshelf.js
使用以下 json:
{
"name" : "Teste",
"username" : "teste1",
"password" : "teste1",
"email" : "teste2@teste",
"photo" : "teste",
"description" : "teste",
"company_posts" : [
{"text": "Oi"},
{"text": "olá"}
]
}
以及以下代码:
company_posts = req.body.company_posts
delete req.body.company_posts
bookshelf.Model.extend({
tableName: 'companies'
}).forge().save(req.body).then(function(company){
for(var prop in company_posts){company_posts[prop].company_id = company.id}
bookshelf.Model.extend({
tableName: 'companies_posts'
}).forge().save(company_posts).then(function(company_posts){
res.send(company_posts)
})
})
服务器returns我:
Unhandled rejection Error: ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
怎么了?如果我尝试插入 company_posts[0]
它会起作用。
如何一次插入所有 company_posts
?
谢谢。
我的错误是试图使用 Model
而不是 Collection
而不是:
bookshelf.Model.extend({
tableName: 'companies_posts'
// where I use forge() instead of collection()
}).forge().save(company_posts).then(function(company_posts){
res.send(company_posts)
})
这很好用:
bookshelf.Model.extend({
tableName: 'companies_posts'
//where I use collection() and works fine
}).collection(company_posts).invokeThen('save').then(function(company_posts){
res.send(company_posts)
})
使用以下 json:
{
"name" : "Teste",
"username" : "teste1",
"password" : "teste1",
"email" : "teste2@teste",
"photo" : "teste",
"description" : "teste",
"company_posts" : [
{"text": "Oi"},
{"text": "olá"}
]
}
以及以下代码:
company_posts = req.body.company_posts
delete req.body.company_posts
bookshelf.Model.extend({
tableName: 'companies'
}).forge().save(req.body).then(function(company){
for(var prop in company_posts){company_posts[prop].company_id = company.id}
bookshelf.Model.extend({
tableName: 'companies_posts'
}).forge().save(company_posts).then(function(company_posts){
res.send(company_posts)
})
})
服务器returns我:
Unhandled rejection Error: ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
怎么了?如果我尝试插入 company_posts[0]
它会起作用。
如何一次插入所有 company_posts
?
谢谢。
我的错误是试图使用 Model
而不是 Collection
而不是:
bookshelf.Model.extend({
tableName: 'companies_posts'
// where I use forge() instead of collection()
}).forge().save(company_posts).then(function(company_posts){
res.send(company_posts)
})
这很好用:
bookshelf.Model.extend({
tableName: 'companies_posts'
//where I use collection() and works fine
}).collection(company_posts).invokeThen('save').then(function(company_posts){
res.send(company_posts)
})