I created a templateTitle method on server side to publish some data from Mongo
Theme = new Mongo.Collection("theme");
if (Meteor.isServer) {
Meteor.startup(function () {
Theme.insert({template: 'booking', value: 'val_example'});
});
Meteor.methods({
templateTitle: function () {
return Theme.findOne({template: 'booking'}, {value:1});
}
});
}
On the client side I try to 'subscribe' that data via calling the templateTitle method - in callback function I would like to save the retrieved value and keep it in reactive variable, but I got a type error here.
Exception in delivering result of invoking 'templateTitle': TypeError: Cannot read property 'title' of null
if (Meteor.isClient) {
Template.booking.created = function() {
this.title = new ReactiveVar('');
}
Template.booking.helpers({
templateTitle: function(){
Meteor.call('templateTitle', function(err, data) {
console.log(data); //data is okey
Template.instance().title.set(data.value); //error on title
});
return Template.instance().title.get();
}
});
}
I tried also this way, but doesn't work as well
if (Meteor.isClient) {
Template.booking.created = function() {
this.title = new ReactiveVar('');
this.autorun(function () {
Meteor.call('templateTitle', function(err, data) {
this.title.set(data.value);
});
});
}
What is wrong with the 'title' variable or the callback function in general?
From the Meteor Docs for Template.instance() :
The template instance corresponding to the current template helper, event handler, callback, or autorun. If there isn't one, null.
I think what's happening in this case is that you're returning the template instance for the current callback (for which there is none, so null
), not the current helper . You should be able to get around this by saving the template instance locally before you make the call to the Method, then referencing that in the callback:
if (Meteor.isClient) {
Template.booking.created = function() {
this.title = new ReactiveVar('');
}
Template.booking.helpers({
templateTitle: function(){
var tmplInst = Template.instance();
Meteor.call('templateTitle', function(err, data) {
console.log(data); //data is okey
tmplInst.title.set(data.value); //error on title
});
return Template.instance().title.get();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.