首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将模式的发布函数分离为server/publications.js

将模式的发布函数分离为server/publications.js
EN

Stack Overflow用户
提问于 2017-02-15 09:24:39
回答 1查看 42关注 0票数 0

我正在尝试将我的应用程序更新为使用Meteors建议的文件结构,而且我很难将发布函数与模式文件分开。我试图使用的文件结构是

代码语言:javascript
复制
imports/
  api/
    profile/                     
      server/
        publications.js
      Profile.js  

当我将发布函数组合到Profile.js模式文件中时,发布函数工作正常,数据流到客户端,但是当我将它们分开时,我无法让它发布。有人能告诉我如何正确地分离发布函数和模式吗?

路径:imports/api/profile/Profile.js

代码语言:javascript
复制
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

代码语言:javascript
复制
Template.main.onCreated(function() {
    this.autorun(() => {
        this.subscribe('private.profile');
    });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-15 10:35:20

如果您导入了集合&确保您的发布被导入到您的服务器上,这应该是可行的:

路径:/imports/api/profile/server/publications.js

代码语言:javascript
复制
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

代码语言:javascript
复制
import '/imports/api/profile/server/publications';

最后,您需要确保将startup/server/index.js导入到服务器。

路径:/server/main.js

代码语言:javascript
复制
import '/imports/startup/server';

如果这让您感到困惑,我建议您在这里阅读“气象厨师”关于导入目录的精彩文章:https://themeteorchef.com/tutorials/understanding-the-imports-directory

而且,这可能看起来很复杂,但坚持下去,你很快就会明白的!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42245291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档