【发布时间】:2015-03-19 21:52:54
【问题描述】:
我在服务器端创建了一个 templateTitle 方法来从 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});
}
});
}
在客户端,我尝试通过调用 templateTitle 方法“订阅”该数据 - 在回调函数中,我想保存检索到的值并将其保存在反应变量中,但我在这里遇到类型错误。
调用'templateTitle'的结果传递异常:TypeError: 无法读取 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();
}
});
}
我也尝试过这种方式,但效果不佳
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);
});
});
}
'title' 变量或回调函数一般有什么问题?
【问题讨论】:
标签: javascript mongodb meteor