Guest User

Untitled

a guest
Aug 3rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import { APIGatewayEvent, Callback, Context, Handler } from "aws-lambda";
  2. import { QueryConfig, Client, Pool, PoolConfig } from "pg";
  3.  
  4. export const insert: Handler = async (
  5. event: APIGatewayEvent,
  6. context: Context,
  7. cb: Callback
  8. ) => {
  9. // context.callbackWaitsForEmptyEventLoop = false;
  10.  
  11. const config: PoolConfig = {
  12. user: process.env.PG_USER,
  13. host: process.env.PG_HOST,
  14. database: process.env.PG_DB,
  15. password: process.env.PG_PASS,
  16. port: parseInt(process.env.PG_PORT),
  17. idleTimeoutMillis: 0,
  18. max: 10000
  19. };
  20.  
  21. const pool = new Pool(config);
  22.  
  23. let postdata = event.body || event;
  24.  
  25. console.log("POST DATA:", postdata);
  26.  
  27. if (typeof postdata == "string") {
  28. postdata = JSON.parse(postdata);
  29. }
  30.  
  31. let query: QueryConfig = <QueryConfig>{
  32. name: "get_all_questions",
  33. text:
  34. "INSERT INTO gamefeedback (gameid, userid, presenterstars, gamestars) VALUES ($1, $2, $3, $4);",
  35. values: [
  36. parseInt(postdata["game_id"]),
  37. postdata["user_id"],
  38. parseInt(postdata["presenter_stars"]),
  39. parseInt(postdata["game_stars"])
  40. ]
  41. };
  42. console.log("Before Connect");
  43.  
  44. let con = await pool.connect();
  45.  
  46. let res = await con.query(query);
  47.  
  48. console.log("res.rowCount:", res.rowCount);
  49.  
  50. if (res.rowCount != 1) {
  51. cb(new Error("Error saving the feedback."), {
  52. statusCode: 400,
  53. body: JSON.stringify({
  54. message: "Error saving data!"
  55. })
  56. });
  57. }
  58.  
  59. cb(null, {
  60. statusCode: 200,
  61. body: JSON.stringify({
  62. message: "Saved successfully!"
  63. })
  64. });
  65. console.log("The End");
  66. };
  67.  
  68. 2018-08-03T15:56:04.326Z b6307573-9735-11e8-a541-950f760c0aa5 (node:1) UnhandledPromiseRejectionWarning: error: sorry, too many clients already
  69. at u.parseE (/var/task/webpack:/node_modules/pg/lib/connection.js:553:1)
  70. at u.parseMessage (/var/task/webpack:/node_modules/pg/lib/connection.js:378:1)
  71. at Socket.<anonymous> (/var/task/webpack:/node_modules/pg/lib/connection.js:119:1)
  72. at emitOne (events.js:116:13)
  73. at Socket.emit (events.js:211:7)
  74. at addChunk (_stream_readable.js:263:12)
  75. at readableAddChunk (_stream_readable.js:250:11)
  76. at Socket.Readable.push (_stream_readable.js:208:10)
  77. at TCP.onread (net.js:607:20)
Add Comment
Please, Sign In to add comment