【问题标题】:MeteorJS I can't seem to ever get a property of an object from the serverMeteorJS 我似乎无法从服务器获取对象的属性
【发布时间】:2015-08-06 15:31:30
【问题描述】:

大家好,我在使用 meteorJS 时遇到了很多问题,因为当我在服务器端订阅发布时,我似乎无法访问任何客户端的属性。我有一个包含“每日活动”的集合,供我的用户使用,我将尝试将其发布到客户端。但由于某种原因,它在客户端未定义,即使我可以在服务器端执行 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')});
});

【问题讨论】:

    标签: javascript web-applications meteor publish-subscribe


    【解决方案1】:

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

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

    var todaysEvent = communityEvents.findOne();
    

    您可以通过打开浏览器控制台并执行fetch 来轻松测试出版物,如下所示:

    communityEvents.find().fetch()
    

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

    【讨论】:

    • 天哪,非常感谢!那行得通。你能向我解释一下光标到底是什么吗?我以前听过这个词,即使在阅读了您链接的文档页面上的 fetch 函数和游标描述之后,我仍然不明白。
    • 我无法在评论中解释。查看this video 以深入了解游标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2017-10-17
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多