我似乎无法连接到 AWS 中的数据库实例。我正在使用该pg软件包,但按照网站上的示例操作不起作用。
搜索“aws postgres 数据库不存在”确实没有返回任何有用的信息。浏览 PG github 上的开放/关闭问题也无济于事。
运行会$nc <RDS endpoint> <port number>返回一条成功消息,所以它肯定存在。客户端配置中的每个值都是从我的数据库实例复制/粘贴的。
我开始怀疑数据库的名称是否与 RDS on AWS 的“实例”部分中显示的名称不同?
const client = new Client({
host : '<<RDS ENDPOINT>>',
database : '<<RDS NAME>>', // maybe this isnt the real name?
user : '<<username>>',
password : '<<password>>',
port : <<port>>
});
client.connect()
.then(data => {
console.log('connected');
})
.catch(err => {
console.log(err);
})
Run Code Online (Sandbox Code Playgroud) 回到节点,但我在连接前端(React)和后端(Node/Express)时遇到问题。我经历了大卫·塞迪亚在他的网站上的文章,它有效,100% 适用于他的示例。
当尝试将相同的逻辑转移到我自己的应用程序中时,我得到了
Proxy error: Could not proxy request /users from localhost:3000 to http://localhost/3001.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).
当然,我找到了标有标签的部分ECONNREFUSED,上面写着
ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host.
所以连接被拒绝,但我不明白为什么。
users.js(我们请求信息的页面)
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function (req, res, next) { …Run Code Online (Sandbox Code Playgroud) 使用节点 pg 包,pg我尝试连接到在 AWS-RDS 中创建的 PostgreSQL 数据库。我相信在创建实例时没有给数据库命名,请注意实例与数据库不同。尝试使用Client或Pool从pg我的语法进行连接时
const client = new Client({
host : <<RDS ENDPOINT>>,
user : <<RDS DB USERNAME>>,
password : <<RDS DB PASSWORD>>,
port : <<RDS DB PORT>>
});
client.connect()
.then(data => {
console.log('connected');
})
.catch(err => {
console.log(err);
})
Run Code Online (Sandbox Code Playgroud)
但每次我带着error: database <<linux user name>> does not exist.
现在创建一个不同的 PostgreSQL 实例并为数据库提供一个名称,我可以database向我的Client对象配置中添加一个道具,一切正常,并返回一个控制台日志connected.
所以我的问题是,我应该如何连接到 AWS-RDS 上的数据库而不database在我的Client配置中提供道具?
在文档中,它显示了readdir和stat的两个版本。两者都有async和sync版本readir/readdirSync和stat/statSync。
因为readidir和stat是异步的,我希望他们返回一个Promise,但是当尝试使用async/await该脚本时,它不会等待readdir解决,如果我使用的话,.then/.catch我会得到一个错误cannot read .then of undefined。
我在这里要做的就是将脚本正在其中运行的目录中存在的目录dirsOfCurrentDir映射到该地图。
cannot read .then of undefinedconst fs = require('fs');
const directory = `${ __dirname }/${ process.argv[2] }`;
const dirsOfCurrentDir = new Map();
fs.readdir(directory, (err, files) => {
let path;
if (err)
return console.log(err);
files.forEach(file => {
path = directory + file;
fs.stat(path, (err, stats) => {
if (err)
return console.log(err); …Run Code Online (Sandbox Code Playgroud) 我有一个对象,其中已经包含很长的属性列表,并且只会不断增长。我正在寻找一种替代方法,即每次需要时仅添加一个新属性,但我不确定是否存在一种替代方法,不仅对开发人员友好,而且非技术熟练的用户也可以轻松进行更改/添加。
我创建了一个小版本的脚本,该脚本应解释正在发生的事情。
const titles = {
fr: 'Some text in french',
fi: 'Some text in finnish'
// tons more properties
}
const locale = window.location.url.split('/')[3] // determines locale to use
const titleElement = document.querySelector('.title')
titleElement.innerText = titles[locale] | 'Some text in english' // sets the titles text
Run Code Online (Sandbox Code Playgroud)
一些上下文会有所帮助。
titles代码段中对象的属性node.js ×3
amazon-rds ×2
postgresql ×2
asynchronous ×1
express ×1
fs ×1
javascript ×1
promise ×1
reactjs ×1