Returning data from async server method onCreated vs onRendered

Curious why the following method call returns undefined in onCreated when a new reply is inserted but if I put the method call in onRendered it works.

Any ideas?

// client
Template.reply.onCreated(function() {
  var instance = this;
  instance.unfurlData = new ReactiveVar();
  let url = //... logic to parse url from Template.currentData()

  Meteor.call('getUnfurl', url, function(error, result) {
    if (error) {
      return error.message
    }
    console.log("result: ", result);
    instance.unfurlData.set(result);
  });
});


// server
Meteor.methods({
  getUnfurl: async function(url) {
    check(url, String);

    try {
      const result = await unfurl(url); // unfurl returns a Promise
      return result;
    } catch (error) {
      throw new Meteor.Error(error.message);
    }
  }
});

Does the logic that generates the url require any element of the template? Have you tried putting logging in your method to check what argument comes through n both cases?

This bit could have something to do with it, as instance.data is a special read-only (Well it’s supposed to be) property that holds props, overwriting it will result in undefined behaviour when accessing data after that point

The logic that generates the url depends on the reply text which I’m getting from Template.currentData(). The reply template is a child that is generated from an each block in the parent.

{{#each replies}}
  {{> reply}}
{{/each}}

I do see the url being parsed correctly on the client before I pass it into the method.

I added a server-side console.log to check the argument. When the method is in onCreated, it appears that it isn’t being called because there’s no console.log of the argument that’s passed in, but the console.log inside the method’s callback on the client returns undefined.

When the method is in onRendered, there is a server-side console.log of the argument.

Ok thanks for the heads up. :slight_smile: I updated to instance.unfurlData but I am seeing the same result.