2017-02-15 28 views
0

我想更新我的应用程序,以利用Meteors建议的文件结构,我无法将发布功能与架构文件分开。我想使用的文件结构单独发布功能我们的架构和进入服务器/ publications.js

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

回答

1

如果导入集合&确保您的出版物被导入到你的服务器这应该工作: /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目录中的文件,然后将该文件导入实际的流星服务器。

所以,你需要导入出版物在/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'; 

如果这让你我建议你阅读TheMeteorChef的真棒文章关于这里的进口商品目录:https://themeteorchef.com/tutorials/understanding-the-imports-directory

而且,这个看似复杂,但坚持下去,你很快就会明白!

+0

谢谢。我一定会读那个tut! – bp123

+0

没有问题。我的答案是否适合你? – Sean

+0

是的。我错过了一堆需要的文件。你指出了。谢谢您的帮助。 – bp123