I am getting an error that request is server denied MapQuest

876 views Asked by At

I am trying to use mapquest api in my nodeJS app but it's giving me this error:

OperationalError: Status is REQUEST_DENIED. You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account

Below is the model:

const geocoder = require('../utils/geocoder');
BootcampSchema.pre('save', async function(next) {
  const loc = await geocoder.geocode(this.address);
  this.location = {
    type: 'Point',
    coordinates: [loc[0].longitude, loc[0].latitude],
    formattedAddress: loc[0].formattedAddress,
    street: loc[0].streetName,
    city: loc[0].city,
    state: loc[0].stateCode,
    zipcode: loc[0].zipcode,
    country: loc[0].countryCode
  };

  // Do not save address in DB
  this.address = undefined;
  next();
});

geocoder utility:

const nodeGeoCoder = require('node-geocoder');

const options = {
  provider: process.env.GEOCODER_PROVIDER,
  httpAdapter: 'https',
  apiKey: process.env.GEOCODER_API_KEY,
  formatter: null
};

const geocoder = nodeGeoCoder(options);

module.exports = geocoder;

API keys are defined in seperate .env file.

How can i solve this issue?

5

There are 5 answers

0
SH SOHEL On BEST ANSWER

You need to call dotenv before routes in sever.js file.

Please follow this:

dotenv.config({ path: './config/config.env' });

const bootcamps = require('./routes/bootcamps');
0
MQBrian On

The error message says, "Google Maps Platform APIs", which is not MapQuest. If there is a MapQuest error, I can help, but this is not a MapQuest API error message. You may want to switch out the MapQuest tag for a Google tag. I hope that helps point in the right direction.

0
vdwivedi On

Looks like the issue is with the Env variables. Had a similar issue. Just use the values in the geocoder.js itself like below. It worked for me.

I created a separate file and then imported and it worked as well. So the issue is with process.env

const options = { provider: 'mapquest', httpAdapter: 'http', apiKey: 'YOUR_API_KEY', formatter: null }

0
Christopher Ortiz Montero On

The problem is when configure dotenv package, in the server.js or index.js file you must be calling the path of the env variables before the routes, so at the top of server.js call directly dotenv and configure the path like this:

require('dotenv').config({ path: './config/config.env' });

Sorry for late, but I hope be helpful.

0
Bismark Obeng On

few suggestions

  1. Check the provider
  2. make sure you require routes after the .env file has been loaded cheers