[SOLVED] Using Meteor.methods and Meteor.call in the imports dir in Meteor 1.3

Sorry if this question already is answered somewhere in here or here, but I didn’t see it.

I am trying to figure out how to use Meteor.methods and Meteor.call with Meteor 1.3 with lazy loading and imports.

For example, if I have this:

import { Mongo } from 'meteor/mongo'
import { Meteor } from 'meteor/meteor'
const Things = new Mongo.Collection('things')

 Meteor.methods({
    '/thing/create': (title) => { 
        // insert into collection
    }
})

export default Things

(I think the issue might be that I need to explicitly export the function I’ve added to Meteor.methods but I’m not clear on how to do that.)

And then I call the method as follows:


import { Meteor } from 'meteor/meteor'
import Things from 'path/to/above'

  handleCreateThing(title) {
  
    Meteor.call('/thing/create', title, (err, result) => {
      if (err) {
        console.log('there was an error: ' + err.reason)
      }
    })
  }

I get a message in my client console that…

there was an error: Method '/thing/create' not found

What am I doing wrong here? How should I properly use Meteor.call and Meteor.methods in Meteor 1.3?

Make sure the file where you define the Meteor.methods is available on the client/server - you can check this with require('/path/to/file');

That file currently is located in /imports/api/collections/things/things.js

Are you recommending that I import the entire file into the client-side file?
eg import '/path/to/above/file'?

I tried that and it had no impact.

Is there a specific reason for using require rather than import?

From looking at your example here, there doesn’t seem to be anything wrong with your code. The first module executes before the second module (because the second module imports the first), so, by the time you need to use Meteor.call, it should just work exactly as you’ve expected.

Notes:

  • The first file with Meteor.methods should always execute on the server side. If it’s on the client only, then, the error makes sense.
  • If the second file imports the first file, but this happens only on the client, then the error makes sense.
  • If both these files run on the server, then your example should work perfectly.

TLDR, requirements for your specific example files:

  • first file must run on the server.
  • both files must run on the client.

(Note, it’s not possible to export the Meteor.methods in order to import them somewhere else, as you thought might have possibly been the problem.)

1 Like

As above, make sure it’s also loaded on the server

Use require instead of import in consoles

2 Likes

Yup, the issue was that I was not also loading the file on the server side as well. So obvious in hindsight.

Thanks all for your help!

1 Like