gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:20:21 27 4
gpt4 key购买 nike

嘿,大家好,我在使用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')});
});

最佳答案

fetch返回一个数组。在你的助手中你需要做类似的事情:

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

var todaysEvent = communityEvents.findOne();

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

communityEvents.find().fetch()

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

关于javascript - MeteorJS 我似乎无法从服务器获取对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com