Meteor.wrapAsync please explain


#1

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'});
      }
    })
  );
}
});

#2

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.


#3

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


#4

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';


#5

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


#6

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


#7

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


#8

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


#9

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


#10
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);
    }

  }
});

#11

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);
    }

  }
});

#12

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


#13

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

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


#14

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


#15

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.


#16

Same problem. Sorry. I really appreciate this help


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

try this. It should work!


#18

And? whats up working now


#19

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


#20

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