Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught Syntax Error: Unexpected token < (in lib/sendgrid.js triggered on a response object) #339

Closed
iamgollum opened this issue Nov 16, 2016 · 10 comments · Fixed by #1303
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@iamgollum
Copy link

Issue Summary

After sending out a templated email using SendGrid's email helper utility. The server response from SendGrid's API is returning invalid JSON according to this wrapper (sendgrid-nodejs).

Uncaught SyntaxError: Unexpected token <
      at Object.parse (native)
      at node_modules/sendgrid/lib/sendgrid.js:110:42
      at IncomingMessage.<anonymous> (node_modules/sendgrid/node_modules/sendgrid-rest/lib/client.js:108:9)
      at endReadableNT (_stream_readable.js:921:12)

Line 110 is:

  //Use callback
  self.client.API(request, function(response) {
    response.body = response.body ? JSON.parse(response.body) : response.body;

Steps to Reproduce

  1. Ensure sendgrid node package -->"sendgrid": "^4.7.1",
  2. Create a template with substitutions in SendGrid and activate that template
  3. Create a full email for the template using the email helper. apply .toJSON at end.

Example:

const BaseWelcomeEmail = (function(){
    const helper = require('sendgrid').mail,
        from_email = new helper.Email(testEmail),
        to_email = new helper.Email(testEmail),
        subject = "Welcome to Bedly",
        content = new helper.Content("text/plain", "hello"),

        mail = new helper.Mail(from_email, subject, to_email, content),
        personalization = new helper.Personalization();

    let substitution = new helper.Substitution(":firstname", "Aaron");

    personalization.addSubstitution(substitution);
    substitution = new helper.Substitution(":lastname", "Elk");
    personalization.addSubstitution(substitution);
    substitution = new helper.Substitution(":address", "96 Washington Avenue");
    personalization.addSubstitution(substitution);
    substitution = new helper.Substitution(":neighborhood", "Washington Heights");
    personalization.addSubstitution(substitution);
    substitution = new helper.Substitution(":unitNumber", "N/A");
    personalization.addSubstitution(substitution);
    substitution = new helper.Substitution(":roomNumber", "2J");
    personalization.addSubstitution(substitution);

    mail.addPersonalization(personalization);
    
    return mail;
})();

You can even create a mocha test, I ran this from this test environment:

    describe('Test Email Template', function () {
        it('test template ${TEMPLATE_ID} is active and is complete', function (done) {
            BaseWelcomeEmail.setTemplateId(TEMPLATE_ID);
            SendEmailImpl.sendEmail(BaseWelcomeEmail.toJSON(), done, function(error, response){
                assert_OKResponse(error, response, done);
            });
        });
    });

The sendEmail function contains the normal POST V3 setup to call the APIas described in the documents.

Technical details:

  • sendgrid-nodejs Version: 4.7.1
  • Node.js Version: 4.6.1
@iamgollum iamgollum changed the title Uncaught Syntax Error: Unexpected token < Uncaught Syntax Error: Unexpected token < (in lib/sendgrid.js triggered on a response object) Nov 16, 2016
@thinkingserious
Copy link
Contributor

Hello @iamgollum,

Thanks for taking the time to submit an issue! I have added this to our backlog for review.

Meanwhile, perhaps this will help: https://github.com/sendgrid/sendgrid-nodejs/blob/master/USE_CASES.md#transactional_templates

Thanks!

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: question question directed at the library labels Nov 17, 2016
@thinkingserious
Copy link
Contributor

@iamgollum,

Are you still having an issue?

@adamsingle
Copy link

I'm having this issue. And I'm using a transactional template as in the link you posted.

@thinkingserious thinkingserious added type: bug bug in the library and removed type: question question directed at the library labels Dec 10, 2016
@thinkingserious
Copy link
Contributor

Hmm, I'm thinking this issue is related: #347

I'm reclassifying this one as a bug and will check them out together.

@thinkingserious
Copy link
Contributor

@adamsingle,

Meanwhile, do you have a code sample we can use to help reproduce? Thanks!

@iamgollum
Copy link
Author

iamgollum commented Jan 21, 2017

@thinkingserious I will try and provide a code sample this weekend. I am coming back to sendgrid now at work and going to revisit everything. I apologize for falling off the face of the earth

@bradseefeld
Copy link

Bah! Have been getting this exact problem for a few hours. Turns out the path is sensitive to the leading slash.

var request = sendgrid.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON()
});

vs

var request = sendgrid.emptyRequest({
   method: 'POST',
   path: 'v3/mail/send',
   body: mail.toJSON()
});

The first snippet works, whereas the second will give you the json parse error :(

We've been a long time sendgrid customer, but just started playing with the api and this lib. Perhaps this detail is well documented and I have overlooked it? Would be nice for the lib to automatically prefix the slash if we omit it.

@thinkingserious
Copy link
Contributor

@bradseefeld,

Sorry you had to deal with that :(

We are working on a better helper that hides all of those details from you. You should not have to care about any of the parameters.

You can follow the project here: https://github.com/sendgrid/sendgrid-nodejs/projects

Thanks!

@bradseefeld
Copy link

No worries. We love the service!

@thinkingserious
Copy link
Contributor

The new helper is in development here: #378

subinoy7 pushed a commit to subinoy7/sendgrid-nodejs that referenced this issue Sep 23, 2021
- Use the replyToList header to set multiple emailIds in the reply-to section of an email
- Fixed old test cases that were failing as SendGrid changed the acceptable response code to 202
shwetha-manvinkurke pushed a commit that referenced this issue Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants