Calling a method from templates onDestroyed event

Methods called from onDestroyed event are not executed on the server and returning undefined result to callback on client. If I call same method from any other event or helper there is no problem.

I just tested this in a project of mine and it works as I would expect. Can you share some code?

Template.action1.onDestroyed(function(){
  var instance = this;
  Meteor.call('onDestroyed',instance.view.name, function(err,res){
    console.log(`onRestroyed ${res}`, err);
  });
});
2 Likes
Meteor.methods({
    "test.Destroy": function(param){
        console.log('testDestroy param:', param);
        Meteor._sleepForMs(3000);
        return "1";        
    },
});
Template.ProductFamilies_SubList_Template.onDestroyed(function() {
    console.log("ProductFamilies_SubList_Template.onDestroyed");
    Meteor.call("test.Destroy", "001", function(error, result){
        if(error){
            console.error("ProductFamilies_SubList_Template error:", error);
        }
        else{
            console.log("ProductFamilies_SubList_Template result:", result);
        }
    });
});

Log prints immediately on the client side;

ProductFamilies_SubList_Template.onDestroyed
ProductFamilies.templates.js:302 ProductFamilies_SubList_Template result: undefined

But nothing at all on the server.

Is your method defined on both, server and client, or only on the server?

Only on the server .

Hmm. Try what you get without the sleep on the server

Same result, method is never called.

That is odd, since my test verified that it should and does work. Is there any other code that might be interfering? Can you call the method outside of onDestroyed?

I implemented my requirement in a different way without depending onDestroyed. But it is quite strange, most of the time it doesn’t work. Sometimes server method is called as expected. Very unreliable. I tried also with an empty project.