Reputation: 8499
How Do I get the User record back, instead of Room record for the transaction below?
bookshelf.transaction(function (t) {
return User.forge(data) // here
.save(null, { transacting: t })
.then(function (user){
return Room.forge({ userId: user.id })
.save(null, { transacting: t });
})
})
Upvotes: 0
Views: 73
Reputation: 3837
Use Promise#tap
.
bookshelf.transaction(function (t) {
return User.forge(data)
.save(null, { transacting: t })
.tap(function (user){ // change here
return Room.forge({ userId: user.id })
.save(null, { transacting: t });
})
})
Note that it will not be attached to the User
model unless you use a relation (probably hasOne
or hasMany
in this case):
bookshelf.transaction(function (t) {
return User.forge(data)
.save(null, { transacting: t })
.tap(function (user){
users.related('rooms').create({}, { transacting: t });
})
})
Upvotes: 1
Reputation: 4182
Add to the chain after the room completes.
bookshelf.transaction(function (t) {
return User.forge(data) // here
.save(null, { transacting: t })
.then(function (user){
return Room.forge({ userId: user.id })
.save(null, { transacting: t })
.then(function (){
return user;
});
})
})
Upvotes: 1