Thursday 10 December 2020

Netflify lambda function working locally but not in production

enter image description here

I'm trying to use netlify and its lambda function feature to run a node function . Based on https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/ , I have in my functions/submission-created.js:

const https = require("https");

exports.handler = async event => {

const email = JSON.parse(event.body).payload.data.EMAIL
const asking = JSON.parse(event.body).payload.data.ASKING

var formData = {
    'email': email,
    'first_name': '',
    'last_name': asking,
    'lists[]': 'NUM'
};
var encoded = Object.entries(formData).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");

var endpoint = 'https://api.sendfox.com/contacts/?' + encoded;

const data = JSON.stringify(formData);
const options = {

    method: 'POST',
    connection: 'keep-alive',

    headers: {
    'Authorization': 'Bearer hhhhh',
    'Content-Type': 'application/json',
    },
    'content-length': data.length,

};
console.log(email);

const req = https.request(endpoint, options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    res.on('data', (d) => {
    console.log(d);
    });
});

req.on('error', (e) => {
    console.error(e);
});

req.write(data);
req.end();

return {
    statusCode: 200,
    body: data
};
}

This works as expected when I run it locally with netlify dev, but when pushed to the github repo used by netlify to build the site, it does not work in production. How can I fix this?

The package structure looks like the screenshot:

EDIT:

the netlify.toml :

[build]
  functions = "./functions

No errors. The output on sites function tab is:

8:57:43 PM: 2020-12-08T01:57:43.384Z    undefined   INFO    to here
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    yt@ghj.org
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    999
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    yt@ghj.org
8:57:43 PM: Duration: 39.71 ms  Memory Usage: 69 MB Init Duration: 
176.22 ms   


from Netflify lambda function working locally but not in production

No comments:

Post a Comment