I haven’t yet pushed this site on netlify but I am using the netlify dev locally to test it.
Here’s some confusion I have… It’s an sendEmail function… very basic… I send the data to the function and then on success, it sends a success response… on error, it send a fail response and on neither I am trying to send a ‘null’ response… I am not sure if this is the right way to go… but that’s another question…
So here’s my code…
import * as dotenv from "dotenv";
dotenv.config();
//
const API_KEY = process.env.MAILGUN_API_KEY;
const DOMAIN = process.env.MAILGUN_DOMAIN;
//
const formData = require("form-data");
const Mailgun = require("mailgun.js");
//
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: "api", key: API_KEY });
//
export const handler = async function (event, context) {
const messageData = JSON.parse(event.body);
// console.log(messageData);
client.messages
.create(DOMAIN, messageData)
.then((res) => {
console.log("test", res);
return {
statusCode: 200,
body: JSON.stringify({ status: "success", data: res }),
};
})
.catch((err) => {
console.error(err);
return {
statusCode: 400,
body: JSON.stringify({ status: "fail", data: err }),
};
});
return {
statusCode: 200,
body: JSON.stringify({ status: null }),
};
};
When I get the response in my client I get a response object which has it’s own keys and values…
here’s the response I get
Response {type: 'basic', url: 'http://localhost:8888/.netlify/functions/sendmail', redirected: false, status: 200, ok: true, …}
so I just check if the status is 200 and then accordingly move ahead… but I was expecting the response that I am sending above in the serverless function… which is an object that has a statusCode and a body…
**1) so why am I not getting the response that I have created? **
2) And where is the default response that I am getting being generated?
3) And is my response being sent as ‘response.body’? If so, it’s a readable stream and I can’t read it.
Any advise on how to modify my mental model of what’s happening here would be a great help.
Regards,
Alim