Running a findOne on a variable holding collection name does not work

Just wanted to know why this is happening and how it can be circumvented.

getInformationOn: function(id,collection) {

       return collection.findOne({_id:id});

Result of calling this function:

I20171109-18:20:57.304(5.5)? Calling 'getInformationOn', arguments: [ 'KrAubggQ8cdf7dGJD', 'policies' ]
I20171109-18:20:57.466(5.5)? Exception while invoking method 'getInformationOn' TypeError: collection.findOne is not a function
I20171109-18:20:57.467(5.5)?     at [object Object].Meteor.methods.getInformationOn (server/methods/getInformationMethod.js:41:25)

It doesn’t work, because the name of a collection is not the collection itself.

Hmm. So then I suppose I’ll have to check which collection it is and then directly query the collection.

or you can pass the collection object to the function…

If you really have to, you can look at Meteor.connection._stores object ( disclaimer: _ is convention for private attributes…)

Sorry, I drew a blank with this. How do I do this?

My turn to apologise, was not clear. I assumed that the caller of the getInformationOn function would have access to the Collection object and could pass that object instead of the collection name string. i.e.,

given that:

PoliciesCollection = new Mongo.Collection ('policies')

then instead of:

let retrievedDocument = getInformationOn('KrAubggQ8cdf7dGJD', 'policies');


let retrievedDocument = getInformationOn('KrAubggQ8cdf7dGJD', PoliciesCollection);

but of course I don’t understand the context of and what you are trying to do (why having a function with a single return statement?), so I was only guessing…