node.jsexpressmongoosedotenvmapquest

I am getting an error that request is server denied MapQuest


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?


Solution

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