MeteorJS 我似乎无法从服务器获得对象的 属性

MeteorJS I can't seem to ever get a property of an object from the server

大家好,我在使用 meteorJS 时遇到了很多问题,因为当我在服务器端订阅发布时,我似乎无法访问任何客户端的属性。我为我的用户准备了一个带有 "daily event" 的集合,我将尝试将其发布到客户端。但由于某种原因,它在客户端未定义,即使我可以在服务器端执行 console.log 并且它工作正常。

代码如下:

Client side:

communityEvents = new Mongo.Collection("communityEvents");
Meteor.subscribe('communityEventsPub');
Template.communityEvent.helpers({
    type: function(){

        todaysEvent = communityEvents.find().fetch();
        console.log("this is the event " + todaysEvent["type"]);
        return todaysEvent.type;
    },
    event: function(){

        todaysEvent = communityEvents.find().fetch();
        return todaysEvent.event;
    }
});

Server Side:

communityEvents = new Mongo.Collection("communityEvents");
Meteor.publish("communityEventsPub", function(){
    console.log(moment().format('l'));
    console.log(communityEvents.find({date:moment().format('l')}).fetch());
    return communityEvents.find({date:moment().format('l')});
});

fetch returns 一个数组。在您的助手中,您需要执行以下操作:

var todaysEvent = communityEvents.find().fetch()[0];

var todaysEvent = communityEvents.findOne();

您可以通过打开浏览器控制台并执行 fetch 来轻松测试发布:

communityEvents.find().fetch()

这将 return 一个您可以检查的数组(希望如此)。