Javascript 在lib文件夹中定义时,客户端中的变量或对象为空
我在lib文件夹中定义了一个不带var的数组userCollections,用新集合填充它:Javascript 在lib文件夹中定义时,客户端中的变量或对象为空,javascript,meteor,Javascript,Meteor,我在lib文件夹中定义了一个不带var的数组userCollections,用新集合填充它: userCollections = []; Collectionlist = CustomCollections.find({}); Collectionlist.forEach(function(col) { userCollections[col.name] = new Meteor.Collection(col.name); Meteor.publish(col.name, func
userCollections = [];
Collectionlist = CustomCollections.find({});
Collectionlist.forEach(function(col) {
userCollections[col.name] = new Meteor.Collection(col.name);
Meteor.publish(col.name, function(){
return Mongo.Collection.get(col.name).find({});
});
console.log(userCollections[col.name]);
});
但是在我的客户机文件夹中,我的数组是空的
console.log(userCollections);
为什么?
问题是:
userCollections = [];
userCollections["0"] = "0_test";
Collectionlist = CustomCollections.find({});
Collectionlist.forEach(function(col) {
userCollections["1"] = "1_test";
console.log(userCollections); //----> array -> "1":"1_test"
});
在my CollectionCursor forEach和my server文件夹中定义了数组
console.log(userCollections); //----> array -> "0":"0_test"
但在我的forEach外部和客户机文件夹中,数组未定义。。。
存在范围问题?您必须在这些频道上订阅客户端。我应该订阅什么?my array->userCollections?我想您不了解订阅在客户端上是如何工作的。如果没有对该集合的订阅,则无法在客户端上使用CustomCollections.find{}。运行meteor AddAutoPublish以查看它是否有效,这只是出于测试目的。阅读这个,好的,但是CustomCollections.find{}在我的lib文件夹中->Collectionlist不是空的。我出版并订阅了CustomCollections。我将使用meteor add autopublish测试它,meteor add autopublish是相同的…我的数组userCollections在forEach 1_测试中具有旧的设置值0_,而不是新的设置值