Sendgrid-nodejs: sendgrid.send not a function

Created on 9 Aug 2016  路  6Comments  路  Source: sendgrid/sendgrid-nodejs

Not sure what is going, for what ever reason, no matter what manner I require('sendgrid') send grid doesn't exist, locally or mounted on the heroku server.

var sendgrid  = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);

// GET /contact --- Tested get and post routes----
router.get('/requestContact', function(req, res, next) {
  sendgrid.send({
    to: 'process.env.email',
    from: '[email protected]',
    subject: 'Hello World',
    text: 'My first email through SendGrid'
  }, function(err, json){
    if(err){return res.send('ahhhh');}
    res.send('yay');
  });
    // return res.json(req.body);
  // return res.render('/', { title: 'Contact' });
});

ReferenceError: sendgrid is not defined
at /Users/jankiewicz/projects/karrietea/routes/index.js:124:3
at Layer.handle as handle_request
at next (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/route.js:112:3)
at Layer.handle as handle_request
at /Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:330:12)
at next (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:176:3)
at router (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:46:12)
at Layer.handle as handle_request
at trim_prefix (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:312:13)
at /Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:330:12)
at next (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:271:10)
at Layer.handle as handle_request
at trim_prefix (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:312:13)
at /Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:330:12)
at next (/Users/jankiewicz/projects/karrietea/node_modules/express/lib/router/index.js:271:10)
at SendStream.error (/Users/jankiewicz/projects/karrietea/node_modules/serve-static/index.js:120:7)
at emitOne (events.js:96:13)

help wanted question

All 6 comments

Hello @Ankiewicz,

How are you installing the SendGrid library?

Chances are you were using sendgrid v2.0, which has now been replaced by version 4.1, which is entirely different. So check out the new docs. You may find that they are a good bit backwards, as I'm finding.

Thanks for jumping in with some support @meteormanaged!

Is there anything we could help you with?

I'm pretty well sorted at the moment. The documentation, on the site, and within the git repo, both need some help though. I'll make some issues or some PRs later today as I can.

Just a quick example from the https://github.com/sendgrid/sendgrid-nodejs#without-mail-helper-class section:

var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: {
    personalizations: [
      {
        to: [
          {
            email: '[email protected]'
          }
        ],
        subject: 'Hello World from the SendGrid Node.js Library!'
      }
    ],
    from: {
      email: '[email protected]'
    },
    content: [
      {
        type: 'text/plain',
        value: 'Hello, Email!'
      }
    ]
  };  <---- syntax error.
});

See inlined note about the syntax error included in the example.

@meteormanaged,

Thanks! I've just fixed that typo.

Issues and/or PRs are most welcome. If you decide to submit a PR, please take a moment to sign our CLA: https://github.com/sendgrid/sendgrid-nodejs/blob/master/CONTRIBUTING.md#cla

Regarding OP:

if you're getting sendgrid is not defined, i'd suggest you verify the ENV variables prior to use:

var sendgrid;
if (process.env.SENDGRID_USERNAME && process.env.SENDGRID_PASSWORD) {
sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
} else {
throw new Error('Env variables: SENDGRID_USERNAME and SENDGRID_PASSWORD are required');
}

Aside from that, ensure your package.json is set to a specific version. The sendgrid.send method no longer appears in the newest versions, but that should return an error specifically regarding that, not undefined as you have shown in your error.

In addition to what you've shown of the route, I'd suggest pasting in your package.json

Was this page helpful?
0 / 5 - 0 ratings

Related issues

metalshan picture metalshan  路  3Comments

agostonbonomi picture agostonbonomi  路  3Comments

nicoasp picture nicoasp  路  3Comments

amlcodes picture amlcodes  路  4Comments

polkhovsky picture polkhovsky  路  3Comments