为什么助手在Meteor中抛出异常

为什么助手在Meteor中抛出异常,meteor,Meteor,流星应用。我有一个模板助手: Template.channel.helpers({ channels: function() { var cursor = News.find({ }, {fields: { title: 1 } }), firstChannel = cursor.fetch()[0]; console.log(firstChannel); Session.set("channelId", firstChannel._id);

流星应用。我有一个模板助手:

Template.channel.helpers({
channels: function() {
    var cursor = News.find({ }, {fields:  { title: 1 } }),
        firstChannel = cursor.fetch()[0];

    console.log(firstChannel);
    Session.set("channelId", firstChannel._id);

    return cursor;
}
}))

我需要在会话中保存channelId,但Meteor抛出异常。浏览器中的此调试器:

undefined
debug.js:41 Exception in template helper: TypeError: Cannot read property '_id' of undefined
    at Object.Template.channel.helpers.channels (http://localhost:3000/client/lib/helpers.js?a55983adb497520dfef7f2a4a8692a7e13e1b4f6:11:46)
    at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2693:16
    at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1602:16
    at Object.Spacebars.call (http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:169:18)
    at http://localhost:3000/client/template.channel.js?44d590e72735f4cdd0f546df5204c94f8cc015f9:12:22
    at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2503:27)
    at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1795:16
    at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2029:12)
    at viewAutorun (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1794:18)
    at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36)
helpers.js?a55983adb497520dfef7f2a4a8692a7e13e1b4f6:10 
Object {title: "Яндекс.Новости: Выставки", _id: "ww2baqF7HhkrDTFA7"}
未定义
debug.js:41模板帮助程序中出现异常:TypeError:无法读取未定义的属性“\u id”
位于Object.Template.channel.helpers.channels(http://localhost:3000/client/lib/helpers.js?a55983adb497520dfef7f2a4a8692a7e13e1b4f6:11:46)
在http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2693:16
在http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1602:16
在Object.spacebar.call(http://localhost:3000/packages/spacebars.js?3c496d2950151d744a8574297b46d2763a123bdf:169:18)
在http://localhost:3000/client/template.channel.js?44d590e72735f4cdd0f546df5204c94f8cc015f9:12:22
在空。(http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2503:27)
在http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1795:16
在Object.Blaze.\u与当前视图(http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2029:12)
在viewAutorun(http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:1794:18)
在Tracker.compute.\u compute(http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36)
helpers.js?a55983adb497520dfef7f2a4a8692a7e13e1b4f6:10
对象{标题:“ww2baqF7HhkrDTFA7”}
为什么console.log(firstChannel)在第一个位置放置“undefined”,在异常之后放置我期望的对象?为什么会抛出异常


谢谢。

您想使用_id,并且您阻止了该字段,只有标题可见

您的新闻文档不存在或尚未到达客户端。您需要在路由控制器中添加新闻订阅,或者需要添加一个。以下是一个简单的解决方案:

Template.channel.helpers({
通道:函数(){
var cursor=News.find({},{fields:{title:1}});
var news=cursor.fetch();
如果(新闻和新闻[0]){
var firstChannel=news[0];
console.log(第一频道);
//请注意,您应该在路线控制器或
//在自动运行中。助手不应有副作用。
Session.set(“channelId”,firstChannel.\u id);
}
返回光标;
}
});
因为助手是被动的,所以当新闻文档可用时,
频道
助手将重新运行


如评论中所述,您的助手不应该有副作用。设置会话变量应在route controller或中完成。

谢谢您的回复。我认为_id在find()中默认返回。我尝试更改为var cursor=News.find({},{fields:{{u id:1,title:1}}}),但没有更改。这可能是真的,可能是我的错,console.log(cursor)是否也返回异常?是的。我不明白这是个什么问题。我已经出版并订阅了。我在客户端上看到我的发布数据:[{u-id:dwedhkfdaf,标题:“utrriedg”},{u-id:ewqewqewqe,标题:“sajdaskjdsal”}…]我不明白为什么第一个文档的{u-id未定义。我正在Tracker.autorun()中插入订阅,我的代码正在运行。非常感谢。