Meteor.wrapAsync please explain

I’m finding using the Meteor.wrapAsync function very confusing and I was hoping someone could show me how to do this. I want to pass the address into a meteor method, which works, and then once the google maps api has returned the address I would like to insert a new collection. The api works and returns the address information, however, I can’t get it to insert a collection. PLEASE HELP!

Path: imports/api/companies/methods.js

Meteor.methods({
  'getAddressDetails'() {
    Meteor.wrapAsync(googleMapsClient.geocode({
      address: 'Test address'
    }, function(err, response) {
      if (!err) {
        console.log(response.json.results[0]);
        Companies.insert({companyName: 'test company'});
      }
    })
  );
}
});
1 Like

Hi! It’s pretty simple. You would need It only if you have to deal with an asynchron function. I guess geocode() would be yours. If the function takes one parameter called address. It would work like this.

const syncSomeAsynchronousFunction = Meteor.wrapAsync(geocode);

    try {

      const response = syncSomeAsynchronousFunction(address);
      const address = response.json.result[0];

     Companies.insert({companyName: 'test company', address: address});


    } catch (err) {
      throw new Meteor.Error(err);
    }

Of course you would need to import your googleMapsClient API on the top of your file.

sorry, noob, what do you mean by Of course you would need to import your googleMapsClient API on the top of your file.

Please explain your code. Where is this coming from “googleMapsClient” ?

You have to bring your api into your method file like so:

import { GoogleMapsClient } from '@google/maps';

This is a method within imports/api/companies/methods.js. I’ve installed the
@google/maps npm package

Ok put the import of the api on top of this file

imports/api/companies/methods.js

You need to import your API, in order to use It’s methods.

Complete code should look like this

import { GoogleMapsClient } from '@google/maps';

Meteor.methods({
  'getAddressDetails'() {

   GoogleMapsClient.createClient({
      key: 'your API key here'
   });

     const syncSomeAsynchronousFunction = Meteor.wrapAsync(GoogleMapsClient.geocode);
     const address = 'Test address';

    try {

      const response = syncSomeAsynchronousFunction(address);
      const address = response.json.result[0];

     Companies.insert({companyName: 'test company', address: address});


    } catch (err) {
      throw new Meteor.Error(err);
    }

  }
});

Is this your API? https://github.com/googlemaps/google-maps-services-js

1 Like

yes that is the api. I’m getting the error ReferenceError: geocode is not defined

Sorry, I didn’t see your update. It now errors on the server Cannot read property 'createClient' of undefined

Please provide your complete code. The reference error means geocode was not found. Thats the case when the GoogleMapsClient was not instantiate correctly.

import SimpleSchema from 'simpl-schema';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Papa from 'papaparse';
import { GoogleMapsClient } from '@google/maps';

import { Companies } from './companies';



Meteor.methods({
  'getAddressDetails'() {

   GoogleMapsClient.createClient({
      key: 'My Key'
   });

     const syncSomeAsynchronousFunction = Meteor.wrapAsync(GoogleMapsClient.geocode);
     const address = 'Moore Stephens Melbounre';

    try {

      const response = syncSomeAsynchronousFunction(address);
      const address = response.json.result[0];

     Companies.insert({companyName: 'test company', address: address});


    } catch (err) {
      throw new Meteor.Error(err);
    }

  }
});

import GoogleMapsClient from ‘@google/maps’;

without brackets sorry

import SimpleSchema from 'simpl-schema';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Papa from 'papaparse';
import GoogleMapsClient from '@google/maps';

import { Companies } from './companies';



Meteor.methods({
  'getAddressDetails'() {

   GoogleMapsClient.createClient({
      key: 'My Key'
   });

     const syncSomeAsynchronousFunction = Meteor.wrapAsync(GoogleMapsClient.geocode);
     const address = 'Moore Stephens Melbounre';

    try {

      const response = syncSomeAsynchronousFunction(address);
      const address = response.json.result[0];

     Companies.insert({companyName: 'test company', address: address});


    } catch (err) {
      throw new Meteor.Error(err);
    }

  }
});
1 Like

getting there. Now I get this error in console. errorClass {isClientSafe: true, error: {…}, reason: undefined, details: undefined, message: "[[object Object]]", …}.

// try to play around with the response (console.log(response) look at the response

const address = response.json.result[0];

It seems to stop running at const response = syncSomeAsynchronousFunction(address);

Try to put the address inside { address }
The api maybe need an object as parameter.

const address = {
address: ‘1600 Amphitheatre Parkway, Mountain View, CA’
};

Yeah, that should work. the parameter shouuld be an object.

Same problem. Sorry. I really appreciate this help

const address = {
  address: 'Moore Stephens Melbounre'
};

try this. It should work!

And? whats up working now

Ok that is weird. Still not working. If I console.log(address) as the first action within try, it doesn’t return anything.

send over your code now. I will have a look at It. You are very close now!