今天,我遇到了一个问题,即无法确定'after delete'
挂钩中的删除操作是否成功完成。这是有问题的,因为它需要额外的数据库操作来确定删除是成功还是失败。
UserFollowers.observe('after delete', function observer(ctx, next) {
var instance = ctx.instance; // ctx.instance is null for some reason.
logger.info('Starting after delete');
logger.info('Ctx:',JSON.stringify(ctx));
// Ctx: {"where":{"followSrcId":2,"followDestId":1},"hookState":{},"options":{}}
logger.info('Ctx Non-Enumerable Properties:', Object.getOwnPropertyNames(ctx));
// Ctx Non-Enumerable Properties: ["Model","where","hookState","options"]
// Note that Model is the only non-enumerable property not spit out by JSON.stringify()
});
没有迹象表明该操作是成功还是失败,并且无论操作成功与否,'after delete'
都会调用该挂钩。(在数据库中手动验证)
此外,确定该操作是否成功的唯一方法是尝试选择在该记录之前存在的记录。
有什么办法可以做到这一点?
您可以设置一个ChangeStream,当服务器上的模型发生更改时,它将发送一个事件。仅警告是在DELETE上,即使没有记录与请求匹配,操作成功时您也会得到一个事件。因此,例如,如果我尝试name=foo
两次删除同一记录,我将把它取回两次:
{"target":"foo","where":{"name":"foo"},"type":"remove"}
https://docs.strongloop.com/display/public/LB/Realtime+server-sent+events
编辑:您还可以通过内置的REST API创建和访问更改流
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句