使用SendGrid的电子邮件助手实用程序发送模板化电子邮件之后。 根据此包装器(sendgrid-nodejs),SendGrid API的服务器响应返回无效的JSON。
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)
第110行是:
//Use callback
self.client.API(request, function(response) {
response.body = response.body ? JSON.parse(response.body) : response.body;
例:
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;
})();
您甚至可以创建一个摩卡测试,我是在此测试环境中运行的:
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);
});
});
});
sendEmail函数包含正常的POST V3设置,以调用文档中所述的API。
@iamgollum ,
您还有问题吗?
我有这个问题。 我使用的是交易模板,如您发布的链接中所示。
嗯,我认为这个问题是相关的: https :
我将这一错误归为错误,并将一起检查。
@adamsingle ,
同时,您是否有一个代码示例可用于帮助重现? 谢谢!
@thinkingserious我将在本周末尝试提供代码示例。 我现在要回到sendgrid上班,并重新审视所有内容。 我为掉下地球的道歉
呸! 已经解决了这个确切的问题了几个小时。 事实证明该路径对前斜杠敏感。
var request = sendgrid.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
与
var request = sendgrid.emptyRequest({
method: 'POST',
path: 'v3/mail/send',
body: mail.toJSON()
});
第一个代码段有效,而第二个代码段将为您提供json解析错误:(
我们一直是sendgrid的长期客户,但是才开始使用api和这个lib。 也许这个细节有据可查,而我却忽略了? 如果我们省略斜杠,它可以自动为斜杠加上前缀。
别担心。 我们热爱服务!
此处正在开发新的帮助程序: https :
最有用的评论
呸! 已经解决了这个确切的问题了几个小时。 事实证明该路径对前斜杠敏感。
与
第一个代码段有效,而第二个代码段将为您提供json解析错误:(
我们一直是sendgrid的长期客户,但是才开始使用api和这个lib。 也许这个细节有据可查,而我却忽略了? 如果我们省略斜杠,它可以自动为斜杠加上前缀。