问题描述
我正在尝试在我的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();
});
geocoder实用程序:
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 Maps Platform API”,而不是MapQuest。如果出现MapQuest错误,我可以提供帮助,但这不是MapQuest API错误消息。您可能希望将MapQuest标记切换为Google标记。我希望这有助于指出正确的方向。
,问题似乎出在 Env 变量上。有一个类似的问题。只需使用 geocoder.js 本身中的值,如下所示。它对我有用。
我创建了一个单独的文件,然后导入,它也能正常工作。所以问题出在 process.env
const 选项 = { 提供者:'mapquest', httpAdapter: 'http',apiKey: 'YOUR_API_KEY',格式化程序:空 }
,问题是在配置dotenv包的时候,在server.js或者index.js文件中必须在routes之前调用env变量的路径,所以在server.js的最上面直接调用dotenv并配置路径像这样:
require('dotenv').config({ path: './config/config.env' });
抱歉来晚了,但希望对您有所帮助。
,一些建议
- 检查提供商
- 确保在加载 .env 文件后需要路由 干杯