将我们的模式的单独发布功能并入server/publications.js [英] Seperate publish function our of schema and into server/publications.js

查看:86
本文介绍了将我们的模式的单独发布功能并入server/publications.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新我的应用程序以利用Meteors建议的文件结构,并且在将发布功能与模式文件分离时遇到了麻烦.我要使用的文件结构是

I'm trying to update my application to utilise Meteors suggested file structure and I'm having trouble separating the publish function from the schema file. the file structure I'm trying to use is

imports/
  api/
    profile/                     
      server/
        publications.js
      Profile.js  

当我将发布功能组合到Profile.js模式文件中时,发布功能有效并且数据流经客户端,但是当我将它们分离时,我无法使其发布.有人可以告诉我如何正确分离发布功能和架构.

When I combine the publish function into the Profile.js schema file the publish function works and the data flows through to the client however when I separate them I'm unable to get it to publish. Can someone please show me how to separate the publish function and schema correctly.

路径: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');
    });
});

推荐答案

如果导入集合&确保您的出版物已导入到您的服务器:

This should work if you import the collection & make sure your publications are being imported to your server:

路径:/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目录中的文件中,然后将该文件导入到实际的流星服务器中.

You need to make sure you are importing your publications file to the server too. No files in the /imports directory are loaded unless they are imported to the server. The way we do this is import all of our publications and methods etc to a file in our /imports/startup/server directory and then we import that file to the actual meteor server.

因此,您需要将出版物导入到/imports/startup/server/index.js文件中

So you need to import the publications in your /imports/startup/server/index.js file

路径:/imports/startup/server/index.js

import '/imports/api/profile/server/publications';

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

And finally you need to make sure your startup/server/index.js is being imported to the server

路径:/server/main.js

import '/imports/startup/server';

如果这让您感到困惑,我建议您在此处阅读TheMeteorChef关于进口目录的真棒文章: https://themeteorchef.com/tutorials/understanding-the-imports-目录

If this is confusing you I recommend you read TheMeteorChef's awesome article about the imports directory here: https://themeteorchef.com/tutorials/understanding-the-imports-directory

此外,这似乎很复杂,但是坚持下去,您很快就会了解!

Also, this may seem complicated but stick with it and you'll understand it soon!

这篇关于将我们的模式的单独发布功能并入server/publications.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆