findbyid and before save

11 views
Skip to first unread message

Claudio

unread,
Jun 18, 2016, 12:39:23 PM6/18/16
to StrongLoop
Hi all,
I'd like to save a copy of a model instance as a new instance of another model BEFORE to delete the former and I used the standard hook:

Thing.observe("before delete", function (ctx, next) {
    console.log("BEFORE delete");

    var id = ctx.where.id;

    Thing.findById(id, function (err, element) {
      if (element)
        console.log("COPY NOW");
      next();
    });

    next();
  });


but I'm fighting with asynchronous methods. How I can ensure to invoke next() only inside the callback of findById ??

Many thanks!
Claudio

Ryan Graham

unread,
Jun 19, 2016, 12:26:27 PM6/19/16
to stron...@googlegroups.com
Have you tried just removing your call to next() that is outside of the findById callback? That should be all that is required, if I understand your question correctly.

~Ryan


Claudio --- [StrongLoop] findbyid and before save ---
--
You received this message because you are subscribed to the Google Groups "StrongLoop" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strongloop+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/strongloop/64d49e3e-47c0-4e15-b255-5d951fae78aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Claudio

unread,
Jul 3, 2016, 3:49:21 AM7/3/16
to StrongLoop
It works!

Many thanks!
Claudio
Reply all
Reply to author
Forward
0 new messages