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;
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.
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!
@iamgollum,
Are you still having an issue?
I'm having this issue. And I'm using a transactional template as in the link you posted.
Hmm, I'm thinking this issue is related: https://github.com/sendgrid/sendgrid-nodejs/issues/347
I'm reclassifying this one as a bug and will check them out together.
@adamsingle,
Meanwhile, do you have a code sample we can use to help reproduce? Thanks!
@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
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.
@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!
No worries. We love the service!
The new helper is in development here: https://github.com/sendgrid/sendgrid-nodejs/pull/378
Most helpful comment
Bah! Have been getting this exact problem for a few hours. Turns out the path is sensitive to the leading slash.
vs
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.