【问题标题】:I am getting an error that request is server denied MapQuest我收到请求被服务器拒绝 MapQuest 的错误
【发布时间】:2021-06-19 04:16:32
【问题描述】:

我正在尝试在我的 nodeJS 应用程序中使用 mapquest api,但它给了我这个错误:

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

下面是模型:

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();
});

地理编码器实用程序:

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 密钥在单独的 .env 文件中定义。

我该如何解决这个问题?

【问题讨论】:

  • 您遇到了 Google 地图错误。你确定这是 MapQuest 吗?也许edit你的问题要澄清?
  • @O.Jones 是 MapQuest。如果我在实用程序文件上对其进行硬编码,它会按预期工作,所以我认为它没有读取 .env 文件。

标签: node.js express mongoose dotenv mapquest


【解决方案1】:

错误消息显示“Google Maps Platform APIs”,这不是 MapQuest。如果出现 MapQuest 错误,我可以提供帮助,但这不是 MapQuest API 错误消息。您可能希望将 MapQuest 标记换成 Google 标记。我希望这有助于指明正确的方向。

【讨论】:

    【解决方案2】:

    看起来问题出在环境变量上。有类似的问题。只需使用 geocoder.js 本身中的值,如下所示。它对我有用。

    我创建了一个单独的文件,然后将其导入,它也能正常工作。所以问题在于 process.env

    常量选项 = { 提供者:'mapquest', httpAdapter: 'http', apiKey: 'YOUR_API_KEY', 格式化程序:空 }

    【讨论】:

      【解决方案3】:

      问题是在配置dotenv包的时候,在server.js或者index.js文件中你必须在路由之前调用环境变量的路径,所以在server.js的顶部直接调用dotenv并配置路径像这样:

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

      对不起,迟到了,但我希望对你有帮助。

      【讨论】:

        【解决方案4】:

        一些建议

        1. 检查供应商
        2. 确保在加载 .env 文件后需要路由 干杯

        【讨论】:

          猜你喜欢
          • 2020-04-19
          • 1970-01-01
          • 2020-05-28
          • 1970-01-01
          • 2017-01-31
          • 2018-11-05
          • 2015-07-29
          • 2023-02-01
          • 1970-01-01
          相关资源
          最近更新 更多