Meteor.wrapAsync please explain


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

   const syncSomeAsynchronousFunction = Meteor.wrapAsync(GoogleMapsClient.geocode);
   
   const address = { 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);
    }
  }
});

Hope your method is in a server directory. Your method file should be in a server directory!

imports/api/companies/methods.js
should be
imports/api/companies/server/methods.js

const address = response;
console.log(address);

It wasn’t so I’ve moved it. I thought methods ran on both server and client. Anyway, same problem. Arrgggh. Address doesn’t seem to pass into try

Yeah. That’s called optimistic ui. Oh man. No error either?

console on client logs the error errorClass {isClientSafe: true, error: {…}, reason: undefined, details: undefined, message: "[[object Object]]", …}

I don’t understand why the address variable doesn’t pass through. If I console.log before try it prints out the correct info.

It’s because It jumps directly to the throw error block.

did you try to put the parameter like so:

const response = syncSomeAsynchronousFunction({address: 'your address'});
response.json.results[0]

TYPO! You have response.json.result[0] in your code. A missing s

hmm. good pickup but try still doesn’t run. OMG!

{isClientSafe: true, error: {…}, reason: undefined, details: undefined, message: "[[object Object]]", …}
} catch (err) {
      throw new Meteor.Error(err.error);
}

And let me now the error again.

Sorry, try is firing. It looks like we’re back to the begging.

const response = syncSomeAsynchronousFunction({address: 'your address'});

appears to be where it stops working.

errorClass {isClientSafe: true, error: undefined, reason: undefined, details: undefined, message: “[undefined]”, …}
details
:
undefined
error
:
undefined
errorType
:
“Meteor.Error"
isClientSafe
:
true
message
:
”[undefined]"
reason
:
undefined
stack
:
"Error: [undefined]↵ at Connection._livedata_result (http://localhost:3000/packages/ddp-client.js?hash=69c1d15adcf9b913cb4704b652adeff4bc462aa8:1975:25)

Ok let’s try the async version of it.

Meteor.methods({
  'getAddressDetails'() {
    GoogleMapsClient.createClient({
      key: 'MyKey'
   });

 // Geocode an address.
GoogleMapsClient.geocode({
  address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
  if (!err) {
    console.log(response.json.results);
  }
});

and what is the log saying now

GoogleMapsClient.geocode is not a function

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

// Geocode an address.
googleMapsClient.geocode({
  address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
  if (!err) {
    console.log(response.json.results);
  }
});
});

try this

If I run it like this, it returns the address. I don’t know why.

var googleMapsClient = require('@google/maps').createClient({
      key: 'my key'
    });

    googleMapsClient.geocode({
      address: '1600 Amphitheatre Parkway, Mountain View, CA'
    }, function(err, response) {
      if (!err) {
        console.log(response.json.results);
      }
    });

TypeError: googleMapsClient.geocode is not a function

ok my final try. Please submit your complete code now. Including imports on the top

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

    // Geocode an address.
    googleMapsClient.geocode({
      address: '1600 Amphitheatre Parkway, Mountain View, CA'
    }, function(err, response) {
      if (!err) {
        console.log(response.json.results);
      }
    });
  }
});
import SimpleSchema from 'simpl-schema';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Papa from 'papaparse';

import { Companies } from '../companies';

Meteor.methods({
  'getAddressDetails'() {

   const googleMapsClient = require('@google/maps').createClient({
      key: 'my key'
    });

    const syncSomeAsynchronousFunction = Meteor.wrapAsync(googleMapsClient.geocode);
   
    const address = { address: 'Moore Stephens Melbounre' };

    try {

      console.log(address);
      const response = syncSomeAsynchronousFunction(address);
      const responseAddress = response.json.results[0];
      console.log(responseAddress);
 
      Companies.insert({companyName: 'test company', address: responseAddress});

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

OMG. It works! Thank you. So it’s all to do with the import of the google api