Home > Back-end >  My nodeJS cloud function crashes on client.request
My nodeJS cloud function crashes on client.request

Time:02-26

trying to make a cloud function NodeJS using HTTP trigger but it always crashes and throws me error 500. Looking into the logs it simply tells me

client.request is not a function

My code looks like the following where I simply take POST request body email and send it to SendGrid. I took the code from SendGrid and tested it locally without being inside a (req, res) request and worked fine.

require('dotenv').config()
const client = require('@sendgrid/mail')
const sendgridKey = process.env.sendgrid_api_key

client.setApiKey(sendgridKey);


exports.welcomeEmail = (req, res) => {
  
  const data = {
    "contacts": [
      {
        "email": req.body.record.email
      }
    ],
    "list_ids": ["somevalue"]
  };
  
  const request = {
    url: `/v3/marketing/contacts`,
    method: 'PUT',
    body: data
  }
  
  client.request(request)
    .then(([response, body]) => {    
      let message = response.body
    return message
    })
    .catch(error => {
      let message = error
    
    return message
    });
  
  res.status(200).send(message);
};

CodePudding user response:

Do you intend to use this package instead?

const client = require('@sendgrid/client');

The example for this package look more like what you want to do than the examples for @sendgrid/mail.

  • Related