Meteor:通过 DDP 连接两个应用程序时,模板助手中没有来自远程集合的文档 [英] Meteor: No docs from remote collection in template helper when connecting two apps via DDP
问题描述
通过 DDP.connect 连接两个 Meteor 应用程序需要以下步骤(据我所知):
- 在远程应用上发布集合
- 通过 DDP.connect 连接到远程应用
- 订阅远程集合(与是通过 DDP.connect 返回的)
- 创建一个新的 Mongo 集合
- 返回帮助文件中集合的文档
- 使用 {{each collectionName}} {{/each}} 显示模板(html 文件)中的文档
我有两个应用程序(一个远程直接连接到 MongoDB,另一个处理所有前端交互的小得多).我可以通过(第 2 步和第 3 步 - 代码在 lib
文件夹中名为 _connect.js 的文件中成功连接它们,以确保它在该文件夹中的任何其他代码之前执行):
if (Meteor.isServer) {如果(process.env.NODE_ENV === '发展'){//开发环境worker_host = '本地主机:4000';//作业工作主机的 IP 地址//连接到作业工作者远程 = DDP.connect(worker_host);}remote.subscribe('cards', function() {console.log('文档数:' + Cards.find().count());});}
当我使用 remote.subscribe
时,它还显示预期文档的数量.这是代码中唯一显示正确文档数的地方,其他地方都显示为 0.
卡片集合通过(步骤 1)正确发布到远程服务器(在端口 4000 上运行):
Meteor.publish('cards', function() {返回 Cards.find()});
第 4 步:我不确定该代码的确切位置.它当前位于 lib
文件夹中,因此该集合在客户端和服务器上都是已知的:
Cards = new Mongo.Collection('cards',remote);console.log('文档数:' + Cards.find().count());
执行此代码时始终显示 0 个文档.
但是,当我想访问模板助手中的相同(远程)集合时(第 5 步 - 文件位于 client
文件夹中):
Template.puzzle.helpers({卡片:函数(){console.log('文档数:' + Cards.find().count());返回 Cards.find();}});
在帮助程序代码中找不到任何文档.
由于上述原因,html 模板中(也在 client
文件夹中)什么都没有显示:
<div class="容器">{{#每张卡片}}//很多 <div>显示卡片文档的内容{{/每个}}
模板>
显然我一定忽略了一些非常简单的东西,我怀疑这是放置我的相关代码的地方.
我需要更改上面代码的哪些步骤才能访问远程集合?我检查了通常的来源(SO,Meteor 论坛),但找不到连接两个应用程序的推荐结构.
PS:我已阅读使用 DDP 连接两个 Meteor 应用程序
正确的方法确实是@Robert Simon 所建议的,决定是仅在服务器上运行它还是仅在客户端上运行它.由于我需要通过客户端显示集合的内容,因此所有内容都必须移动到客户端.
这些是使其工作的
第 2/3 步(将 _connect.js 文件移动到 client
文件夹:
worker_host = 'localhost:4000';//作业工作主机的 IP 地址//连接到作业工作者远程 = DDP.connect(worker_host);remote.subscribe('cards', function() {console.log('卡片数量:' + Cards.find().count());});
第 4 步(将 card.js 文件移动到 client
文件夹:
Cards = new Mongo.Collection('cards',remote);
当第一次执行拼图.js 文件(帮助程序)时,它不显示任何卡片,因为应用程序运行速度如此之快,以至于没有发生通过 DDP 的数据传输.在随后的第二次运行中,所有文档都在帮助程序中接收并显示在 html 文件中.
Connecting two Meteor apps via DDP.connect requires the following steps (to my knowledge):
- Publish the collection on the remote app
- Connect via DDP.connect to the remote app
- Subscribe to the remote collection (same object that was returned via DDP.connect)
- Create a new Mongo collection
- Return the docs of the collection in the helper file
- Displaying the docs in the template (html file) with {{each collectionName}} {{/each}}
I have two apps (one remote on that is connected directly to the MongoDB and one much smaller one that is handling all the frontend interactions). I can successfully manage to connect them via (step 2 & 3 - the code is in the lib
folder in a file called _connect.js to ensure it's executed before any other code in that folder):
if (Meteor.isServer) {
if (process.env.NODE_ENV === 'development') {
// development environment
worker_host = 'localhost:4000'; // ip address of job worker host
// connect to job worker
remote = DDP.connect(worker_host);
}
remote.subscribe('cards', function() {
console.log('Number of docs: ' + Cards.find().count());
});
}
and it also displays the number of expected docs when I then use remote.subscribe
. This is the only place in the code where it shows the correct number of docs, everywhere else it shows 0.
The cards collection is properly published on the remote server (which is running on port 4000) via (step 1):
Meteor.publish('cards', function() {
return Cards.find()
});
Step 4: I'm unsure where exactly to place this code. It's currently in the lib
folder so that the collection is known both on client and server:
Cards = new Mongo.Collection('cards',remote);
console.log('Number of docs: ' + Cards.find().count());
It always shows 0 docs when this code is executed.
However when I want to access the same (remote) collection in the template helper (step 5 - file is in client
folder):
Template.puzzle.helpers({
cards: function() {
console.log('Number of docs: ' + Cards.find().count());
return Cards.find();
}
});
Not a single doc is found in the helper code.
For the above reason nothing is displayed in the html template (also in client
folder):
<template name="puzzle">
<div class="container">
{{#each cards}}
// lot's of <div> displaying the content of the cards doc
{{/each}}
</div>
</template>
Obviously I must have overlooked something very simple, I suspect it's where my relevant code is being placed.
What steps of the above code do I need to change to access the remote collection? I've checked the usual sourced (SO, Meteor forum) but can't find the recommended structure for connecting two apps.
PS: I've read Connect two Meteor applications using DDP
The correct way to do this is indeed what @Robert Simon has suggested, make a decision to either run it on the server only or on the client only. As I need to display the contents of the collection via the client, everything had to be moved to the client.
These are the edits that made it work:
Step 2/3 (moved the _connect.js file to client
folder:
worker_host = 'localhost:4000'; // ip address of job worker host
// connect to job worker
remote = DDP.connect(worker_host);
remote.subscribe('cards', function() {
console.log('Number of cards1: ' + Cards.find().count());
});
Step 4 (moved the cards.js file to client
folder:
Cards = new Mongo.Collection('cards',remote);
When the puzzle.js file (the helper) is executed the first time it's not displaying any cards as the app runs through it so fast that no transfer of data via DDP has happened. On a subsequent second run all docs are received in the helper and get displayed in the html file.
这篇关于Meteor:通过 DDP 连接两个应用程序时,模板助手中没有来自远程集合的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!