我正在尝试将我的应用程序更新为使用Meteors建议的文件结构,而且我很难将发布函数与模式文件分开。我试图使用的文件结构是
imports/
api/
profile/
server/
publications.js
Profile.js 当我将发布函数组合到Profile.js模式文件中时,发布函数工作正常,数据流到客户端,但是当我将它们分开时,我无法让它发布。有人能告诉我如何正确地分离发布函数和模式吗?
路径:imports/api/profile/Profile.js
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { AddressSchema } from '../../api/profile/AddressSchema.js';
import { ContactNumberSchema } from '../../api/profile/ContactNumberSchema.js';
export const Profile = new Mongo.Collection("profile");
Profile.allow({
insert: function(userId, doc) {
return !!userId;
},
update: function(userId, doc) {
return !!userId;
},
remove: function(userId, doc) {
return !!userId;
}
});
var Schemas = {};
Schemas.Profile = new SimpleSchema({
userId: {
type: String,
optional: true
},
firstName: {
type: String,
optional: false,
},
familyName: {
type: String,
optional: false
}
});
Profile.attachSchema(Schemas.Profile);
if (Meteor.isServer) {
Meteor.publish('private.profile', function() {
return Profile.find({});
});
}路径:client/main.js
Template.main.onCreated(function() {
this.autorun(() => {
this.subscribe('private.profile');
});
});发布于 2017-02-15 10:35:20
如果您导入了集合&确保您的发布被导入到您的服务器上,这应该是可行的:
路径:/imports/api/profile/server/publications.js
import { Profile } from '/imports/api/profile/profile';
Meteor.publish('private.profile', function() {
return Profile.find({});
});您也需要确保将发布文件导入服务器。除非将文件导入服务器,否则不会加载/imports目录中的任何文件。我们这样做的方式是将所有发布和方法等导入到/imports/startup/server目录中的文件中,然后将该文件导入到实际的meteor服务器。
所以您需要导入/imports/startup/server/index.js文件中的发布。
路径:/imports/startup/server/index.js
import '/imports/api/profile/server/publications';最后,您需要确保将startup/server/index.js导入到服务器。
路径:/server/main.js
import '/imports/startup/server';如果这让您感到困惑,我建议您在这里阅读“气象厨师”关于导入目录的精彩文章:https://themeteorchef.com/tutorials/understanding-the-imports-directory
而且,这可能看起来很复杂,但坚持下去,你很快就会明白的!
https://stackoverflow.com/questions/42245291
复制相似问题