我无法理解Ctx.instance和Ctx.data在回环“保存前”回调中的区别。
发布于 2021-03-12 15:40:23
我们的文档中描述了这种差异,请参阅Operation hooks >> before save。在此交叉张贴相关内容:
根据触发此钩子的方法,上下文将具有下列属性之一:
- (...)
- `instance` - the model instance to be saved. The value is an instance of Model class.- (...)
- `data` - the (partial) data to apply during the update
- `currentInstance` - the affected instance.调用Customer.create({/*...*/})时,通过将ctx.instance设置为模型实例来调用钩子,并从传递给create()的对象中填充所有属性。
{
name: 'Miroslav',
isPreferred: false,
// etc.
} 当您调用customer.patchAttributes({isPreferred: true})时,将使用只包含提供给patchAttributes()的属性的ctx.data调用钩子。
{
isPreferred: true
}您可以在源代码中了解更多信息,请参见loopback-datasource-juggler/lib/dao.js。
https://stackoverflow.com/questions/66352243
复制相似问题