Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { APIGatewayEvent, Callback, Context, Handler } from "aws-lambda";
- import { QueryConfig, Client, Pool, PoolConfig } from "pg";
- export const insert: Handler = async (
- event: APIGatewayEvent,
- context: Context,
- cb: Callback
- ) => {
- // context.callbackWaitsForEmptyEventLoop = false;
- const config: PoolConfig = {
- user: process.env.PG_USER,
- host: process.env.PG_HOST,
- database: process.env.PG_DB,
- password: process.env.PG_PASS,
- port: parseInt(process.env.PG_PORT),
- idleTimeoutMillis: 0,
- max: 10000
- };
- const pool = new Pool(config);
- let postdata = event.body || event;
- console.log("POST DATA:", postdata);
- if (typeof postdata == "string") {
- postdata = JSON.parse(postdata);
- }
- let query: QueryConfig = <QueryConfig>{
- name: "get_all_questions",
- text:
- "INSERT INTO gamefeedback (gameid, userid, presenterstars, gamestars) VALUES ($1, $2, $3, $4);",
- values: [
- parseInt(postdata["game_id"]),
- postdata["user_id"],
- parseInt(postdata["presenter_stars"]),
- parseInt(postdata["game_stars"])
- ]
- };
- console.log("Before Connect");
- let con = await pool.connect();
- let res = await con.query(query);
- console.log("res.rowCount:", res.rowCount);
- if (res.rowCount != 1) {
- cb(new Error("Error saving the feedback."), {
- statusCode: 400,
- body: JSON.stringify({
- message: "Error saving data!"
- })
- });
- }
- cb(null, {
- statusCode: 200,
- body: JSON.stringify({
- message: "Saved successfully!"
- })
- });
- console.log("The End");
- };
- 2018-08-03T15:56:04.326Z b6307573-9735-11e8-a541-950f760c0aa5 (node:1) UnhandledPromiseRejectionWarning: error: sorry, too many clients already
- at u.parseE (/var/task/webpack:/node_modules/pg/lib/connection.js:553:1)
- at u.parseMessage (/var/task/webpack:/node_modules/pg/lib/connection.js:378:1)
- at Socket.<anonymous> (/var/task/webpack:/node_modules/pg/lib/connection.js:119:1)
- at emitOne (events.js:116:13)
- at Socket.emit (events.js:211:7)
- at addChunk (_stream_readable.js:263:12)
- at readableAddChunk (_stream_readable.js:250:11)
- at Socket.Readable.push (_stream_readable.js:208:10)
- at TCP.onread (net.js:607:20)
Add Comment
Please, Sign In to add comment