Meteor js - 即使返回结果,控制台也会显示 'Undefined'
Meteor js - Console shows 'Undefined' even if returning result
我有以下用于简单帖子存档的助手:
Template.archive.helpers({
itens: function () {
return Itens.find();
}
});
//singleExcerpt is the single item on archive loop
Template.singleExcerpt.helpers({
shortExcerpt: function() {
var a = this.text.slice(0,120);
return a+'...';
}
})
并且在存档页面上,它列出了所有帖子及其 shortExcerpt
的 120 个字符,但它仍然 returns 在控制台上未定义:
Exception in template helper: TypeError: Cannot read property 'slice' of undefined
有谁知道这可能是什么问题?
使用RegisterHelper
http://docs.meteor.com/#/full/template_registerhelper
Template.registerHelper('shortExcerpt', function(text, limit) {
return text.substring(0, limit) + (text.length > limit ? '...' : '');
});
Template.archive.helpers({
itens: function () {
return Itens.find();
}
});
<template name="archive">
{{# each itens }}
{{ shortExcerpt text 150}}
{{/each}}
</template>
使用所有模板
{{ shortExcerpt 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.' 80 }}
--> "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adi..."
尝试这样的事情:
Template.archive.helpers({
shortExcerpt: function(text) {
if(text.length>120){
var a = text.slice(0,120);
return a+'...';
}
return text;
}
})
<template name="archive">
{{# each itens }}
{{ shortExcerpt text }}
{{/each}}
</template>
所以,在问题评论中讨论后,恰好是数据存在的问题——一些集合文档根本就没有shortExcerpt
字段。
要解决这个问题,要么使用 a package such as aldeed:collection2
强制存在该字段,要么自己进行一些数据验证...
您还可以选择在您的模板中考虑它:
Template.singleExcerpt.helpers({
shortExcerpt: function() {
var a = '';
if(typeof this.text !== 'undefined') {
a = this.text.slice(0,120);
}
return a + '...';
}
});
但是,我建议您确保所有文档都具有完全相同的结构。在这种情况下,这意味着必须始终定义 shortExcerpt
,即使只包含一个空字符串。
我有以下用于简单帖子存档的助手:
Template.archive.helpers({
itens: function () {
return Itens.find();
}
});
//singleExcerpt is the single item on archive loop
Template.singleExcerpt.helpers({
shortExcerpt: function() {
var a = this.text.slice(0,120);
return a+'...';
}
})
并且在存档页面上,它列出了所有帖子及其 shortExcerpt
的 120 个字符,但它仍然 returns 在控制台上未定义:
Exception in template helper: TypeError: Cannot read property 'slice' of undefined
有谁知道这可能是什么问题?
使用RegisterHelper http://docs.meteor.com/#/full/template_registerhelper
Template.registerHelper('shortExcerpt', function(text, limit) {
return text.substring(0, limit) + (text.length > limit ? '...' : '');
});
Template.archive.helpers({
itens: function () {
return Itens.find();
}
});
<template name="archive">
{{# each itens }}
{{ shortExcerpt text 150}}
{{/each}}
</template>
使用所有模板
{{ shortExcerpt 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.' 80 }}
--> "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adi..."
尝试这样的事情:
Template.archive.helpers({
shortExcerpt: function(text) {
if(text.length>120){
var a = text.slice(0,120);
return a+'...';
}
return text;
}
})
<template name="archive">
{{# each itens }}
{{ shortExcerpt text }}
{{/each}}
</template>
所以,在问题评论中讨论后,恰好是数据存在的问题——一些集合文档根本就没有shortExcerpt
字段。
要解决这个问题,要么使用 a package such as aldeed:collection2
强制存在该字段,要么自己进行一些数据验证...
您还可以选择在您的模板中考虑它:
Template.singleExcerpt.helpers({
shortExcerpt: function() {
var a = '';
if(typeof this.text !== 'undefined') {
a = this.text.slice(0,120);
}
return a + '...';
}
});
但是,我建议您确保所有文档都具有完全相同的结构。在这种情况下,这意味着必须始终定义 shortExcerpt
,即使只包含一个空字符串。