meteor-astronomy icon indicating copy to clipboard operation
meteor-astronomy copied to clipboard

Cannot read property 'apply' of null with local collection and meteorMethods

Open davidsavoie1 opened this issue 8 years ago • 3 comments

Hi,

I'm trying to develop an application where my collections are local only, and grounded. Here's an example:

const Circuits = new Ground.Collection('circuits', { connection: null });

The problem is that if I declare a method in the meteorMethods of a Class linked to this collection, I get this error message when I call it from the client:

Uncaught TypeError: Cannot read property 'apply' of null
    at callStandardMeteorMethod (callStandardMeteorMethod.js:7)
    at Class.applyMethod (applyMethod.js:18)
    at Class.start (wrapMethod.js:23)
    at TestCircuitPage.startCirc (TestCircuitPage.js:5)
    at Function.setValueFull (modules.js?hash=d000c37…:10004)
    at Object.setVmValue (modules.js?hash=d000c37…:9979)
    at HTMLButtonElement.eventListener (modules.js?hash=d000c37…:13039)

If I declare my method in a standard Meteor Method however, and call it like so:

Meteor.call('start', (err, res) => {...});

everything works fine. Also, if I use standard collections (linked to the database), everything works as intended too...

Is there something happening when converting from meteorMethods to standard Meteor methods that could cause such a problem? Thanks in advance!

davidsavoie1 avatar Jul 07 '17 17:07 davidsavoie1

The workaround I've found for the moment is to use plain helpers instead of meteorMethods so Meteor doesn't try to execute anything server-side. For my use case, it's probably all very well, but I'm still wondering if there would have been another solution.

Thanks for the helping hand!

davidsavoie1 avatar Jul 07 '17 17:07 davidsavoie1

Hey, could you create reproduction repository? I've never been using grounded collections, so don't know right now why it's not working. There is a need for having connection object in the collection.

function callStandardMeteorMethod(Class, methodName, methodArgs, callback) {
  const Collection = Class.getCollection();
  let connection = Collection && Collection._connection;
  if (!connection && (!Collection || !Collection._name)) {
    connection = Meteor.connection;
  }
  return connection.apply(methodName, methodArgs, callback);
}

export default callStandardMeteorMethod;

As you can see in this code, if there is not connection object in the collection then it falls back to Meteor.connection so, in theory, it should work. But in your case it looks like there is no Meteor.connection which is really strange. Are you using this locally without Meteor server?

lukejagodzinski avatar Jul 08 '17 19:07 lukejagodzinski

I'll try to make a repro soon... Sorry for the delay!

davidsavoie1 avatar Jul 19 '17 12:07 davidsavoie1