Confused about how to read the response from a serverless function

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

I think my issue was in fact because of my not understanding the workings of ‘promises’ very well…

I changed my code to the async/await method and it works as I expected…

My understanding of the ‘then’ part of a promise and what it returns may be flawed…

Anyways here’s the modified code and it works fine

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);

  const sentConfirmation = await client.messages.create(DOMAIN, messageData);

  return {
    statusCode: 200,
    body: JSON.stringify({ status: "success", data: sentConfirmation }),
  };
};

In my earlier code, I was returning it mulitple times (or at least thinking that I was) but in fact it might have been returning just the last one…

Hi @alimbolar :wave:t6:, welcome to the forums! Thanks for coming back and letting us know what solution you used! We appreciate the feedback as this will help other users who encounter similar problems.

1 Like