I am using node-googlemaps to query the Google Maps API from Node js. I have created my Server API key as a Developer. According to the this documentation, I have to make a publicConfig variable with my API key and certain other parameters. My publicConfig variable is as follows:
var gm = require('googlemaps');
var publicConfig = {
key: 'myKey',
stagger_time:1000, // for elevationPath
encode_polylines:false,
secure:true // use https
};
var gmAPI = new GoogleMapsAPI(publicConfig);
When I run the Code, I get the following error
ReferenceError: GoogleMapsAPI is not defined
Can somebody help me on this issue?
To Resolve this issue I had include the following line of Code
var GoogleMapsAPI = require('googlemaps');
Related
I have Google Maps API Key and want to use it with google-distance-matrix library. But i don't know where to put my Key and integrate it with above stated library here is my code
const distanceAPI = require('google-distance-matrix')
dispatchers.dispatchers.map( (dispatcher) => {
distanceAPI.matrix(dispatcher.location.coordinates, call.pickupLocationCoordinates.coordinates, mode, function(err, distances) {
console.log("distances")
console.log(distances)
})
})
Following error is still being thrown
error = 'You must use an API key to authenticate each request to Google Maps Platform APIs.'
I have also create a variable in .env file with following
G_API = key
Kindly let me know the right way of using the map service.
As mentioned in the README.md of google-distance-matrix you have to precise your key in distance.key() method.
distance.key('myAPIkey');
If your using a .env file you should call your env variable variable with process.env and dotenv package.
index.js :
require('dotenv').config();
const distance = require('google-distance-matrix');
distance.key(process.env.MY_API_KEY);
// Working example code
var origins = ['San Francisco CA'];
var destinations = ['New York NY', '41.8337329,-87.7321554'];
distance.matrix(origins, destinations, function (err, distances) {
if (!err)
console.log(distances);
})
.env :
MY_API_KEY=1234AEB
You can check a full working example on the google-distance-matrix doc
here
I have the following test application based on a template provided by openshift.
server.js:
var express = require('express');
exports.NodeTestApp = function () {
self.cache_get = function (key) {
return 'Would be a value here';
};
}
server_test.js:
var server = require('../server');
describe('Server', function(){
describe('Startup',function(){
it('sets up routes during startup',function(){
var app = server.NodeTestApp();
app.cache_get('/');
expect(app.routes.size).to.equals(5);
})
})
})
When I run this test I get an error message that cache_get is not defined.
TypeError: Cannot read property 'cache_get' of undefined
at Context.<anonymous> (test/server_test.js:7:16)
I would have thought that everything that is specified in the NodeTestApp function is available via variable app. IntelliJ even shows me the function as a valid call. Any idea why I get this error ?
Thanks in advance.
Oliver
I figured out the problem with above code. To instantiate the app variable the new keyword has to be used. I didn't think that this is required as I would have thought its purely a function call and not a constructor call, which requires the new keyword. Below is the working code for your reference.
var app = new server.NodeTestApp();
I started a new project from scratch with ExpressJS.
Everything works fine but now I begin to have a dozen of 'app.get(....)' function and I need to give the project a structure.
What I have in mind is quite simple, it should have a folder named 'routes' containing a file such as 'module1.js', with all of the app.get related to that module. (like I've seen in many examples)
The issue is how to tell Express to route 'http://url/module1/' to that route file and how to pass it a param variable, containing for instance the mongodb connection.
what I tried is :
var params = {
db: myMongoConnection
};
var mod1 = require('routes/module1');
app.use('/module1', mod1);
but now I still miss the 'params'.
If I try to pass it as an argument to the require method i get an error saying it needs middleware.
Another issue is related to the fact that the myMongoConnection is valid in the connection callback, so I think i need to require and use the route.js inside the MongoClient connect callback.
Any idea?
thanks a lot
For custom modules, create a folder, call it modules
In its index.js, expose the modules that you need.
Something like,
var mods = [
'mod1',
'mod2',
];
function init() {
var expose = {};
var params = {
db: myMongoConnection
};
mods.forEach(mods, function (mod) {
expose[mod] = require('./' + mod)(params);
});
return expose;
}
// export init
module.exports = init;
In mod1.js, wrap the params
module.exports = function(params) {
// all your functions here will have access to params.
}
Then in, server/app.js, require this and set it in the app.
app.set('mods', require('path-to/modules'));
Now, you can access all your modules, using app.get('mods').moduleName.methodname
I'm trying to get the POST data using koa-body-parser but I get the following error :
SyntaxError: Unexpected token e
at Object.parse (native)
This error refer to
/co-body/node_modules/raw-body/index.js
I think that the library co-body is trying to use "parse" but in my node version this function is restricted.
I'm using node 0.11.13
This is a part of the app.js
var path=require('path');
var koa = require('koa');
var app = koa();
app.use(require('koa-body-parser')());
//enrutamiento
app.use(require('./configs/routes')(app));
This is the function that recibe the call:
function *(){
/*
var str = 'email=lopezchr%40gmail.com&password=123123';
console.log(JSON.parse(str));
*/
var self = this;
var attributes= this.request.body
var userModel = this.models.user;
userModel.create(this.request.body).exec(function(){
self.body={success:true,description:"user Created"}
});
}
Aditionally, when I try to do this:
var str = 'email=lopezchr%40gmail.com&password=123123';
console.log(JSON.parse(str));
I optain the same error..
update
In other post, I realized that string is not a JSON.. sooo... that is the problem...
I'm trying to do this:
$.post('/auth',$(form).serialize(),function(data){
console.log(data);
});
And I want to recibe the form data with koa-body-parce...What should I do?
For some reazon, the jquery function $.post was sending the message with type json.. so that caused the error.. now the message type is plain/text and works.. thanks
I'm trying to get a very simple test of Amazon SimpleDB running with Node.js/Express. This is the code I'm using (AWS key/secret sanitized, of course):
var express = require('express');
var simpledb = require('simpledb');
var app = express.createServer();
var sdb = new simpledb.SimpleDB(
{keyed:'MYKEY', secret:'MYSECRET'}, simpledb.debuglogger);
app.get('/', function(req, res) {
console.log("about to list domains...");
sdb.listDomains(function(error, result, meta) {
console.log("listing domains, I think?");
});
});
app.listen(8888);
This is the error I'm getting:
DEBUG: simpledb: 2012-04-06T01:34:24.856Z create {"keyid":"MYKEY","secret":"MYSECRET","secure":false,"consistent":true,"test":false,"maxtry":null,"expbase":null,"delaymin":null,"delayscale":null,"randomdelay":null} {"secure":false,"host":"sdb.amazonaws.com","path":"/","version":"2009-04-15","port":80}
about to list domains...
DEBUG: simpledb: 2012-04-06T01:34:29.253Z request 1333676069253 ListDomains {}
DEBUG: simpledb: 2012-04-06T01:34:29.387Z handle 1333676069253 ListDomains {"Action":"ListDomains","Version":"2009-04-15","SignatureMethod":"HmacSHA256","SignatureVersion":"2","Timestamp":"2012-04-06T01:34:29.253Z","AWSAccessKeyId":"MYKEY","Signature":"AWSSIGNATURE"} 1 false null
/home/rob/node_modules/simpledb/lib/simpledb.js:136
if( res.Errors ) {
^
TypeError: Cannot read property 'Errors' of null
at [object Object].handle (/home/rob/node_modules/simpledb/lib/simpledb.js:136:12)
at /home/rob/node_modules/simpledb/lib/simpledb.js:188:18
at Parser.<anonymous> (/home/rob/node_modules/simpledb/node_modules/aws-lib/lib/aws.js:81:13)
at Parser.emit (events.js:67:17)
at Object.onclosetag (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/xml2js/lib/xml2js.js:120:24)
at emit (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/sax/lib/sax.js:148:32)
at emitNode (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/sax/lib/sax.js:152:3)
at closeTag (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/sax/lib/sax.js:226:5)
at Object.write (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/sax/lib/sax.js:567:29)
at Parser.<anonymous> (/home/rob/node_modules/simpledb/node_modules/aws-lib/node_modules/xml2js/lib/xml2js.js:145:29)
I'm pretty new to Node.js, the simpledb module and SimpleDB itself, but this to me seems like a bug in the simpledb module. I can't figure out what I'm doing wrong otherwise - I'm confident my key/secret are valid (as I've tested with both set invalid, separately and together, and I get back actual errors from Amazon that indicate the key/secret are invalid).
This error, though, has me stumped. Any ideas?
This turned out to be a dependency issue in the simpledb node module:
Hi - this seems to have been caused by the latest version of a
dependent lib - I've rebuilt and publish a new version 0.0.8 - please
let me know if this works - thanks!
Source. It has since been fixed.
FYI - Since your original post, AWS has published their official SDK for Node.js. http://docs.aws.amazon.com/nodejs/latest/dg/nodejs-dg-examples.html
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var db = new AWS.SimpleDB();
db.client.listDomains(function(error, data) {
if (error) {
console.log(error);
} else {
console.log(data);
}
});