如何解决Node JS中的Lambda函数在Visual Studio代码中使用SAM插件并尝试连接到PG实例
嗨,我正在使用sam插件在vscode中运行lambda代码。节点js 12.x中使用的运行时。在本地postgresql实例中使用的数据库。 通过SAM无法连接到数据库。我的代码正在安装了docker台式机的Windows 10笔记本电脑上运行。
'use strict';
var pg = require('pg');
var connection = new pg.Client({
host : 'localhost',user : 'postgres',password : 'admin',database : 'logtest'
});
module.exports.hello = (event,context,callback) => {
connection.connect();
connection.query('SELECT 1 + 1 AS solution',function (error,results,fields) {
if (error) throw error;
console.log('The solution is: ',results[0].solution);
const response = {
statusCode: 200,body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',solution: results[0].solution,}),};
connection.end();
callback(null,response);
});
};
我得到的错误是
2020-09-24T15:24:26.796Z a127f8c8-1526-1fb9-fa7c-4fcbceda7929 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: connect ECONNREFUSED 127.0.0.1:5432","reason":{"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:5432","code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5432,"stack":["Error: connect ECONNREFUSED 127.0.0.1:5432"," at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: connect ECONNREFUSED 127.0.0.1:5432"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:315:20)"," at processpromiseRejections (internal/process/promises.js:209:33)"," at processticksAndRejections (internal/process/task_queues.js:98:32)"]}
Function 'HelloWorldFunction' timed out after 3 s
我尝试使用docker.for.win.localhost和host.docker.internal而不是localhost,但是没有成功。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。