Meteor.JS: 订阅无效
Meteor.JS: Subscribe no working
我正在尝试将我的客户端订阅到我的 userFriends 集合和 Chrome 的控制台显示:userFriends is not defined
这是我的代码:
服务器端...
userFriends = new Mongo.Collection("friends");
console.log(userFriends.find().fetch())
Meteor.publish("friends", function () {
return userFriends.find();
});
注意: console.log 在终端中显示一个空数组,这很好
客户端...
Meteor.subscribe("friends");
console.log(userFriends.find().fetch())
注意: 这是 Chrome 的控制台显示错误的地方
我做错了什么?
谢谢
更新 1:现在我可以在 Chrome 的控制台中看到 Friends 集合,但我无法插入数据。我在我的客户端文件夹内的 client.js 中订阅,我的插入代码也在客户端文件夹内的 friend.js 中。
需要在 客户端和服务器上定义该集合。通常这是通过将定义放在共享目录中来完成的,例如 lib
:
lib/collections/user-friends.js
userFriends = new Mongo.Collection('friends');
请注意,惯例是使用集合名称的大写驼峰形式命名集合。所以称它为Friends
会更典型。
您需要使用共享代码在两个环境中声明 collection。
lib/user-friends.js
userFriends = new Mongo.Collection("friends");
client/user-friends.js
Meteor.subscribe("friends", function(){
console.log(userFriends.find().fetch());
});
在客户端中,请注意 collection 订阅本质上是异步的(客户端存在网络延迟,这是从服务器获取文档所固有的)。
这就是为什么如果您在 Meteor.subscribe
之后 console.log
您的 collection 内容,您将获得 []
,但如果您等到订阅准备好使用一个回调,文档将正确显示。
您有两个正确答案,但它们确实为您假定了一些知识。这是使用 Meteor 的文件结构(可在 http://docs.meteor.com/#/full/structuringyourapp 获得)的样子。
在您的 /lib(共享)目录中
创建一个名为 "collections.js" 的文件并在其中创建您的 collection.
userFriends = new Mongo.Collection("friends");
我会改为 userFriends = new Mongo.Collection("userfriends");这样您的 collection 总是使用同一个词,并且根据您是在客户端还是服务器上工作来更改大小写。这很有帮助。
在您的 /client 目录中
创建一个名为 "subscriptions.js" 的文件并在其中订阅您的 collection。
Meteor.subscribe('friends');
在您的 /server 目录中
创建一个名为 "publications.js" 的文件并在其中发布您的 collection。
Meteor.publish('friends',function(){
return userFriends.find();
});
那里不需要提取或任何东西。
本质上,您的代码失败是因为您试图将所有内容放置在何处。我给你的是你工作地点的三点。客户端、共享、服务器。以这种方式设置您的应用程序,将很容易立即找出您的工作地点。
希望对您有所帮助。
我正在尝试将我的客户端订阅到我的 userFriends 集合和 Chrome 的控制台显示:userFriends is not defined
这是我的代码:
服务器端...
userFriends = new Mongo.Collection("friends");
console.log(userFriends.find().fetch())
Meteor.publish("friends", function () {
return userFriends.find();
});
注意: console.log 在终端中显示一个空数组,这很好
客户端...
Meteor.subscribe("friends");
console.log(userFriends.find().fetch())
注意: 这是 Chrome 的控制台显示错误的地方
我做错了什么?
谢谢
更新 1:现在我可以在 Chrome 的控制台中看到 Friends 集合,但我无法插入数据。我在我的客户端文件夹内的 client.js 中订阅,我的插入代码也在客户端文件夹内的 friend.js 中。
需要在 客户端和服务器上定义该集合。通常这是通过将定义放在共享目录中来完成的,例如 lib
:
lib/collections/user-friends.js
userFriends = new Mongo.Collection('friends');
请注意,惯例是使用集合名称的大写驼峰形式命名集合。所以称它为Friends
会更典型。
您需要使用共享代码在两个环境中声明 collection。
lib/user-friends.js
userFriends = new Mongo.Collection("friends");
client/user-friends.js
Meteor.subscribe("friends", function(){
console.log(userFriends.find().fetch());
});
在客户端中,请注意 collection 订阅本质上是异步的(客户端存在网络延迟,这是从服务器获取文档所固有的)。
这就是为什么如果您在 Meteor.subscribe
之后 console.log
您的 collection 内容,您将获得 []
,但如果您等到订阅准备好使用一个回调,文档将正确显示。
您有两个正确答案,但它们确实为您假定了一些知识。这是使用 Meteor 的文件结构(可在 http://docs.meteor.com/#/full/structuringyourapp 获得)的样子。
在您的 /lib(共享)目录中
创建一个名为 "collections.js" 的文件并在其中创建您的 collection.
userFriends = new Mongo.Collection("friends");
我会改为 userFriends = new Mongo.Collection("userfriends");这样您的 collection 总是使用同一个词,并且根据您是在客户端还是服务器上工作来更改大小写。这很有帮助。
在您的 /client 目录中
创建一个名为 "subscriptions.js" 的文件并在其中订阅您的 collection。
Meteor.subscribe('friends');
在您的 /server 目录中
创建一个名为 "publications.js" 的文件并在其中发布您的 collection。
Meteor.publish('friends',function(){ return userFriends.find(); });
那里不需要提取或任何东西。
本质上,您的代码失败是因为您试图将所有内容放置在何处。我给你的是你工作地点的三点。客户端、共享、服务器。以这种方式设置您的应用程序,将很容易立即找出您的工作地点。
希望对您有所帮助。