简体   繁体   English

查询另一个Meteor js应用中的一个应用创建的集合

[英]Querying collection created by an app in another Meteor js app

I have 2 apps that query the same db, they both created diff collections on a db. 我有2个查询相同数据库的应用程序,它们都在数据库上创建了diff集合。 App A at myapp:3000 created a collection comments while app B at myapp:3300 created courses at its end in the same db ( school ). myapp:3000应用程序A创建了集合commentsmyapp:3300应用程序B在其末尾的同一数据库( school )中创建了courses both servers are running, how do I query the a collection created by app A in app B? 两个服务器都在运行,如何查询由应用A在应用B中创建的集合?

My reason for asking this question is that, as a newbie, it is easy for me to query new collection created in an app by using it global variable. 我问这个问题的原因是,作为一个新手,我很容易通过使用全局变量来查询在应用程序中创建的新集合。 Let's say I created 假设我创建了

UsesList = new Mongo.Collection('useslist');

it is possible for me to publish it this way: 我可能以这种方式发布它:

Meteor.publish('UsesList', function () {
        if (!this.userId) {
            return false;
            throw new Meteor.Error('Not authorized');
        } else {
            return UsesList.find();
        }
    });

I wouldn't have access to this variable, and I think accessing the database directly to query the collection might be unethical. 我将无法访问此变量,并且我认为直接访问数据库以查询集合可能是不道德的。

No problem in "creating" the same collection in two different applications. 在两个不同的应用程序中“创建”相同的集合没有问题。

The collection identity is determined by the name string you provide to the collection constructor, and the identity of the database itself, of course. 集合标识由提供给集合构造函数的名称字符串以及数据库本身的标识确定。

You can assign them to whatever variable identifier you want, and they may be different between the applications. 您可以将它们分配给所需的任何变量标识符,并且它们在应用程序之间可能有所不同。

I would, however, recommend you to start using ES6 modules and reduce global namespace pollution. 但是,我建议您开始使用ES6模块并减少全局名称空间污染。 Start using the best practices when you are learning, so you won't have to unlearn the bad ones later. 在学习时就开始使用最佳实践,因此您以后不必再学习不良实践。

So, provided both apps are connected to the same DB: 因此,假设两个应用程序都连接到同一数据库:

// same in app 1 and app 2
import { Mongo } from 'meteor/mongo';
export const UsesList = new Mongo.Collection('user_list');

// and importing it
import { UserList } from '../path/to/other/file';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM