>我在 Mongo 中有一个包含 2 个项目的集合,启用自动发布时会看到它们。但是当我禁用自动发布以及添加发布和订阅代码时,它不再起作用。
这是我第一次使用 Meteor 版本 1.7.0.5,在我总是使用 1.6 之前,我从来没有遇到过发布/订阅任何问题......
这是一个如此简单的测试,但我做错了什么?我有以下代码和文件:
/client/xxx/xxx.html
<template name="XxxTemplate">
{{#each xxxHelper}}
{{name}}<br>
{{/each}}
</template>
/集合/_Xxxx.js
import SimpleSchema from 'simpl-schema'
XxxCollection = new Meteor.Collection('XxxCollection');
XxxCollectionSchema = new SimpleSchema({
name: {
type: String,
label: "Name"
}
});
XxxCollection.attachSchema(XxxCollectionSchema);
/server/mongodb/publish.js
Meteor.publish('XxxCollection', function () {
return XxxCollection.find();
});
/client/xxx/xxx.js
Template.XxxTemplate.onCreated(function() {
Meteor.subscribe('XxxCollection');
});
Template.XxxTemplate.helpers({
xxxHelper: function() {
console.log("xxxHelper is called");
var r = XxxCollection.find();
console.log(r);
return r;
}
});
我的 package.json 文件如下所示:
{
"name":"TestApp",
"private":true,
"scripts":{
"start":"meteor run",
"test":"...",
"test-app":"...",
"visualize":"meteor --production --extra-packages bundle-visualizer"
},
"dependencies":{
"@babel/runtime":"7.0.0-beta.55",
"meteor-node-stubs":"^0.4.1",
"simpl-schema":"^1.5.3"
},
"meteor":{
"mainModule":{
"client":"client/main.js",
"server":"server/main.js"
},
"testModule":"tests/main.js"
}
}
如果你想让你的项目像在 Meteor 1.6 中一样工作,你必须从你的 package.json 中删除 mainModule 属性。
解释:
从Meteor 1.7开始,新项目默认启用延迟加载,即使在 imports/文件夹之外也是如此。
这是由 package.json 文件中的属性 mainModule 完成的:
"mainModule": {
"client": "client/main.js",
"server": "server/main.js"
},
如果你想使用预先加载(禁用延迟加载(,你必须从你的package.json中删除mainModule属性。
在您的情况下,问题不是来自自动发布,而是来自启用的延迟加载。
更多资源在这里:
流星使用 ES 模块的指南:https://guide.meteor.com/structure.html#es2015-modules
流星博客文章关于 1.7:https://blog.meteor.com/meteor-1-7-and-the-evergreen-dream-a8c1270b0901