У меня возникла проблема, и я не могу понять, в чем дело.
У меня есть лямбда-функция nodejs, и первый запрос прекрасно работает через шлюз API, но для всех последующих запросов это дает сбой при следующем:
response body:
{
"message": "Internal server error"
}
error message:
Tue Feb 04 11:11:57 UTC 2020 : Received response. Status: 200, Integration latency: 13 ms
Tue Feb 04 11:11:57 UTC 2020 : Endpoint response headers: {Date=Tue, 04 Feb 2020 11:11:57 GMT, Content-Type=application/json, Content-Length=4, Connection=keep-alive, x-amzn-RequestId=d49a531a-74f6-4f9b-bce4-989e3c1bdbef, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5e39517d-e160bf769d459b3acfe72e31;sampled=0}
Tue Feb 04 11:11:57 UTC 2020 : Endpoint response body before transformations: null
Tue Feb 04 11:11:57 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Tue Feb 04 11:11:57 UTC 2020 : Gateway response type: DEFAULT_5XX with status code: 502
Tue Feb 04 11:11:57 UTC 2020 : Gateway response body: {"message": "Internal server error"}
Моя лямбда-функция записывается следующим образом:
const pg = require("pg");
const dbConfig = require('./config/db');
const format = require('pg-format');
const client = new pg.Client(dbConfig);
client.connect();
module.exports.putrequests = (event, context, callback) => {
var body = JSON.parse(event.body);
var dates = body.date;
var md5 = body.md5;
var url = body.url;
var email = body.email;
var prepost = body.prepost;
var status = "pending";
const sql = 'INSERT into requests (date, md5, url, email, prepost) values ($1,$2,$3,$4,$5)';
const values = [dates, md5, url, email, prepost];
const query = client.query(sql, values, (err, res) => {
if (err) {
console.log(err.stack);
callback(null, {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: "Could not insert service " + err.stack
});
} else {
console.log(res.rows[0]);
}
});
query.on('row', (row, result) => {
result.addRow(row);
});
query.on('end', result => {
var jsonString = JSON.stringify(result.rows);
client.end();
callback(null, {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
"body": "DB Insertion Successful"
});
});
query.on('error', res => {
callback(null, {
statusCode: res.statusCode || 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: "Could not insert service " + res.stack
});
});
};
Кто-нибудь есть какие-либо идеи, почему это происходит при последующих запросах?