【发布时间】:2015-06-02 03:32:36
【问题描述】:
我在我的代码中实现了一个“保存前”操作挂钩,以将要保存的新实例与数据库中已有的旧实例进行比较。 为此,我将 ctx.data 中给出的值与数据库中查询给出的值进行比较。 问题是返回的值总是相似的,就好像新实例已经保存在数据库中一样。 我是否完全错过了“保存前”钩子的要点,还是有办法比较这两个值?
module.exports = function(app) {
var Like = app.models.Like;
Like.observe('before save', function(ctx, next) {
var count = 0;
if (ctx.instance) { // create operation
console.log('create operation);
}
else { // update operation
// Query for the existing model in db
Like.findById(ctx.where.id,
function(err, item) {
if (err)
console.log(err);
else {//compare query value and instance value
if (item.value != ctx.data.value) {
// Always false
}
else {
//Always true
}
}
}
);
}
next();
我不明白为什么 item.value 总是与 ctx.data.value 相似,因为第一个应该是数据库中的实际值,第二个应该是要保存的值。
【问题讨论】:
标签: loopbackjs strongloop