Loopback: Hooks: Unable to delete a context.instance property

Created on 4 Mar 2015  路  11Comments  路  Source: strongloop/loopback

I try to clean up some data after processing it into my context object, using:

Let's say ctx.instance contains the following:

{
  unwantedField: "MY_SUPER_MESSAGE",
  something: "ok",
  anotherThing: "not ok"
}
MyModel.observe('before save', function doSomethingOnDB(ctx, next) {
  if (ctx.instance) {
    //do something on insert, like filling another DB or calling a webservice
    //then delete not wanted properties so they are not stored in my DB
    ctx.instance.unwantedField = undefined;
    //tried delete either
    delete ctx.instance.unwantedField;
  }
  next();
});

But it appears that ctx.instance.unwantedField is stored in my DB anyway with value "MY_SUPER_MESSAGE"
Setting it to null insert it with null value, but I have no way to completely remove it and prevent insertion into the database.

bug

Most helpful comment

This is a known issue - it's why unsetAttribute exists. Try it instead of delete ctx.instance.unwantedField; like this:

ctx.instance.unsetAttribute('unwantedField');

All 11 comments

@PuKoren thank you for reporting the issue. What model method is triggering your "before save" hook? Could you please provide us a full code to reproduce the problem, e.g. as described in https://github.com/strongloop/loopback/wiki/Reporting-issues?

Thanks for your answer, the method triggering the hook is the simple built-in POST method.
I'll create the repo asap

There is the repo:
https://github.com/PuKoren/loopback-sandbox

Steps to reproduce:

  • Clone the repo and start the server
  • POST on /MyModels with the following body
    {"WantedProperty": "My Value", "UnwantedProperty": "Value 2"}
  • GET on /MyModels and see the result (UnwantedProperty is still there)

This is a known issue - it's why unsetAttribute exists. Try it instead of delete ctx.instance.unwantedField; like this:

ctx.instance.unsetAttribute('unwantedField');

This works, thanks.

@crandmck perhaps unsetAttribute should be more clearly documented somewhere. The reason why delete inst.prop doesn't work, is because of the internal object that stores the actual attribute values.

Added to http://docs.strongloop.com/display/LB/Operation+hooks#Operationhooks-Operationhookcontextobject. Thanks for the heads up!
BTW, this is an area where we want to improve the docs in general - see #1173.

@crandmck actually, it's not tied to operation hooks at all. In general, if you ever want to remove a property from a model instance, this is the way to do it. So I think it should move up to a more basic level of the docs (working with models?).

Oh, ok; thanks. My assumption was based on the example code above. How do you get the ctx object? Can you provide an example of using this outside of a hook?

@crandmck sure:

var inst = new Thing({ foo: 'bar' });
inst.unsetAttribute('foo');
console.log(inst.foo);  // undefined
console.log(inst.toObject()); // {}

Is unsetAttribute deprecated? I cannot see it in loopback v3.

Was this page helpful?
0 / 5 - 0 ratings