Hi,
Does anyone know an easy way to access a collection that is declared within the shared lib directory in a meteor based project (project/lib/collections.js) from within the code of a meteor plugin/package? Is there any method such as Mongo.getCollection?
I need to be able to make a new reactive publication for that collection and also subscribe to it from a template declared within a package, and no I cannot alter the code within the main meteor project.
Is there even a way to do this or is it meant to be impossible for some security reasons? Or have i just missed something obvious?
This is what I have looked at:
const db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
And I have come up with the following code for the server side:
function mongoFindCall(collectionName, query, what) {
return new Promise(function(resolve, reject) {
db.collection(collectionName).find(query, what).toArray(
function(err, items) {
if (err) {
reject(err);
} else {
resolve(items);
}
}
);
});
}
Meteor.publish('messages', function() {
let subscription = this;
mongoFindCall('messages', {}, {}).then(function(messages) {
messages.forEach(message => {
subscription.added("messages", new Meteor.Collection.ObjectID()._str, message);
});
subscription.ready();
}, function(err) {
console.error('The promise was rejected', err, err.stack);
});
});
The problem is that the code is not reactive. And the next question is if that could be made reactive, how do I serve the data in the template? I have subscribed to the publication using this code within Template onCreated and autorun:
Meteor.subscribe('messages', {
onReady: function () { console.log(arguments); },
onError: function () { console.log("onError", arguments); }
})