Reputation: 647
Hello i'm using lambda function to update my database every 2 minutes but I don't know why it doesn't work correctly. When I open the URL in my browser I get all my console logs but when lambda sends the request nothing logs in my console. What's the problem?
I'm using apollo-expres-graphql for the backend and react for frontend.
lambda function:
const https = require('https');
exports.handler = async (event) => {
let dataString = '';
const response = await new Promise((resolve, reject) => {
const req = https.get("https://example.com/api/req-update", function(res) {
res.on('data', chunk => {
dataString += chunk;
});
res.on('end', () => {
resolve({
statusCode: 200,
body: JSON.stringify('Success!')
});
});
});
req.on('error', (e) => {
reject({
statusCode: 500,
body: 'Something went wrong!'
});
});
});
return response;
};
Upvotes: 1
Views: 438
Reputation: 1178
I dont understand your question. Do you mean that the URL in https.get is expected to return you some logs and when you call the URL directly it shows you the logs but this lambda does not return logs? If this is what you are asking, then your async/await promise is not properly setup. Also you are not returning dataString. Change it to this --
const https = require('https');
async function httpGet() {
let dataString = '';
const response = await new Promise((resolve, reject) => {
const req = https.get("https://example.com/api/req-update",
function(res) {
res.on('data', chunk => {
dataString += chunk;
});
res.on('end', () => {
resolve({
statusCode: 200,
body: JSON.stringify('Success!') // return dataString here
});
});
});
req.on('error', (e) => {
reject({
statusCode: 500,
body: 'Something went wrong!'
});
});
});
return response;
}
exports.handler = async (event) => {
return await httpGet();
};
Upvotes: 1