bp123
March 10, 2018, 2:13am
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'});
}
})
);
}
});
1 Like
xvendo
March 10, 2018, 2:28am
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.
bp123
March 10, 2018, 2:38am
3
sorry, noob, what do you mean by Of course you would need to import your googleMapsClient API on the top of your file.
xvendo
March 10, 2018, 2:38am
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';
bp123
March 10, 2018, 2:42am
5
This is a method within imports/api/companies/methods.js
. I’ve installed the
@google/maps npm package
xvendo
March 10, 2018, 2:44am
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
1 Like
bp123
March 10, 2018, 2:52am
7
yes that is the api. I’m getting the error ReferenceError: geocode is not defined
bp123
March 10, 2018, 2:54am
8
Sorry, I didn’t see your update. It now errors on the server Cannot read property 'createClient' of undefined
xvendo
March 10, 2018, 2:56am
9
Please provide your complete code. The reference error means geocode was not found. Thats the case when the GoogleMapsClient was not instantiate correctly.
bp123
March 10, 2018, 3:00am
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);
}
}
});
xvendo
March 10, 2018, 3:00am
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);
}
}
});
1 Like
bp123
March 10, 2018, 3:03am
12
getting there. Now I get this error in console. errorClass {isClientSafe: true, error: {…}, reason: undefined, details: undefined, message: "[[object Object]]", …}
.
xvendo
March 10, 2018, 3:05am
13
// try to play around with the response (console.log(response) look at the response
const address = response.json.result[0];
bp123
March 10, 2018, 3:07am
14
It seems to stop running at const response = syncSomeAsynchronousFunction(address);
xvendo
March 10, 2018, 3:09am
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.
bp123
March 10, 2018, 3:10am
16
Same problem. Sorry. I really appreciate this help
xvendo
March 10, 2018, 3:11am
17
const address = {
address: 'Moore Stephens Melbounre'
};
try this. It should work!
xvendo
March 10, 2018, 3:15am
18
And? whats up working now
bp123
March 10, 2018, 3:16am
19
Ok that is weird. Still not working. If I console.log(address) as the first action within try
, it doesn’t return anything.
xvendo
March 10, 2018, 3:17am
20
send over your code now. I will have a look at It. You are very close now!