我是使用sails JS的新手。
我有这样的方法
SELECT country FROM campaigns WHERE campaign_id = 1
当我试图销毁数据书'IsBorrowed'时仍然如此,如何在尝试删除数据时解决此问题,首先找到id,然后将数据书IsBorrowed更改为false?感谢Advance
答案 0 :(得分:0)
这是一个解决方案(对于您原来的问题 - 只需按现在的需要切换isBorrowed逻辑):
book.js
module.exports = {
schema: true,
attributes: {
name: {type: 'string', required: true},
desc: {type: 'text'},
isBorrowed: {type: 'boolean', defaultsTo: false}
}
};
bookBorrowed.js
module.exports = {
schema: true,
attributes: {
book: {model: 'Book'}
},
beforeDestroy: function (criteria, next) {
var bookId = criteria.where.id;
console.log(bookId);
Book.update({id: bookId}, {isBorrowed: true})
.exec(function (err, updatedBook) {
if (err) {
next('no book..');
}
console.log('updated book', updatedBook);
next();
});
}
};
你的问题是你应该考虑与id的关系,而不是对象。 另外,传入beforeDestroy的criteria参数有一个where对象,它不是模型。此外,update()函数采用对象标准,见上文。
如果您想测试,请使用以下代码段替换您的bootstrap.js:
module.exports.bootstrap = function (cb) {
var bk = {name: 'name', desc: 'desc', isBorrowed: false};
Book.create(bk).exec(function (err, book) {
if (err) {
cb(err);
}
console.log('book: ', book);
var bb = {book: book.id};
BookBorrowed.create(bb).exec(function (err, bkBorrowed) {
if (err) {
cb(err);
}
console.log('bkBorrowed: ', bkBorrowed);
BookBorrowed.destroy({id: bkBorrowed.id}).exec(function (err, bkBorrowed) {
if (err) {
cb(err);
}
cb();
});
})
});
};