简体   繁体   中英

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. 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 ). both servers are running, how do I query the a collection created by app A in app 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. 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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