无论我做啥,都会在 Node.js 中不断收到“ConfigError:Missing region in config”错误
Posted
技术标签:
【中文标题】无论我做啥,都会在 Node.js 中不断收到“ConfigError:Missing region in config”错误【英文标题】:Keep getting the "ConfigError: Missing region in config" error in Node.js no matter what I do无论我做什么,都会在 Node.js 中不断收到“ConfigError:Missing region in config”错误 【发布时间】:2019-09-06 16:19:45 【问题描述】:尝试向我在 Node.js 中设置的 API 发出请求时,我不断收到“UnhandledPromiseRejectionWarning: ConfigError: Missing region in config”。
我是 DynamoDB 的新手,在设置了大部分样板代码后,我正在使用 Postman 来测试我的路线。但是,每次发出发布请求时,我都会收到相同的错误。我已经检查了现有线程上的一些解决方案,即:Configuring region in Node.js AWS SDK 但无法使其正常工作。
我目前正在本地开发应用程序并检查了添加项目的数据库。
我的设置如下:
// user_controller.js
const uuid = require('uuid');
const sanitizer = require('validator');
const bcrypt = require('bcryptjs-then');
const AWS = require('aws-sdk');
const config = require('../config/config');
const signToken, userByEmail, userById = require('../Helpers/Users');
const isDev = true
然后在我的代码块中,我有以下内容:
// user_controller.js
(...)
if (isDev)
AWS.config.update(config.aws_local_config);
else
AWS.config.update(config.aws_remote_config);
const DB = new AWS.DynamoDB.DocumentClient();
const params =
TableName: config.aws_table_name,
Item:
userId: await uuid.v1(),
firstName: sanitizer.trim(firstName),
lastName: sanitizer.trim(lastName),
email: sanitizer.normalizeEmail(sanitizer.trim(email)),
password: await bcrypt.hash(password, 8),
level: 'standard',
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
,
return userByEmail(params.Item.email) // Does the email already exist?
.then(user => if (user) throw new Error('User with that email exists') )
.then(() => DB.put(params).promise()) // Add the data to the DB
.then(() => userById(params.Item.id)) // Get user data from DB
.then(user => (err, data) =>
console.log("AFTER USER CREATED")
if (err)
res.send(
success: false,
message: 'Error: Server error'
);
else
console.log('data', data);
res.send(
statusCode: 201,
message: 'Success - you are now registered',
data: token: signToken(params.Item.id), ...user ,
);
)
(...)
最后我从单独的文件中导入配置:
// config.js
module.exports =
aws_table_name: 'usersTable',
aws_local_config:
region: 'local',
endpoint: 'http://localhost:8000'
,
aws_remote_config:
已经配置了aws-sdk:
AWS Access Key ID [****************foo]:
AWS Secret Access Key [****************bar]:
Default region name [local]:
Default output format [json]:
这是我不断得到的输出:
(node:4568) UnhandledPromiseRejectionWarning: ConfigError: Missing region in config
at Request.VALIDATE_REGION (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/event_listeners.js:92:45)
at Request.callListeners (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at callNextListener (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
at /Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/event_listeners.js:86:9
at finish (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/config.js:350:7)
at /Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/config.js:368:9
at SharedIniFileCredentials.get (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/credentials.js:127:7)
at getAsyncCredentials (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/config.js:362:24)
at Config.getCredentials (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/config.js:382:9)
at Request.VALIDATE_CREDENTIALS (/Users/BANGBIZ/Programming/techstars/capexmove/SmartLegalContract/node_modules/aws-sdk/lib/event_listeners.js:81:26)
(node:4568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:4568) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
就像我说的,我在这方面尝试了很多变体,但都无济于事。希望得到一些帮助,谢谢。
【问题讨论】:
另见:Dynamo Error "ConfigError: Missing region in config" when developing Alexa skill locally 【参考方案1】:我不知道这是否有帮助,但我在该地区使用 none
而不是 local
,它似乎对我有用
AWS.config.update( region: 'none' )
【讨论】:
以上是关于无论我做啥,都会在 Node.js 中不断收到“ConfigError:Missing region in config”错误的主要内容,如果未能解决你的问题,请参考以下文章
无论我做啥,我都会在打印时遇到语法错误。这是下面的代码,我找不到问题所在