SendGridのメールヘルパーユーティリティを使用してテンプレート化されたメールを送信した後。 SendGridのAPIからのサーバー応答は、このラッパー(sendgrid-nodejs)に従って無効な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関数には、ドキュメントで説明されているAPIを呼び出すための通常のPOSTV3セットアップが含まれています。
こんにちは@iamgollum 、
問題の提出に時間を割いていただきありがとうございます。 これをレビューのためにバックログに追加しました。
一方、おそらくこれは役立つでしょう: https :
ありがとう!
@iamgollum 、
まだ問題がありますか?
この問題が発生しています。 そして、私はあなたが投稿したリンクのようにトランザクションテンプレートを使用しています。
うーん、この問題は関連していると思います: //github.com/sendgrid/sendgrid-nodejs/issues/347
これをバグとして再分類し、一緒にチェックします。
@adamsingle 、
一方、再現に役立つコードサンプルはありますか? ありがとう!
@thinkingserious今週末にコードサンプルを提供しようと思います。 私は今仕事でsendgridに戻ってきて、すべてを再訪するつもりです。 地球の表面から落ちたことをお詫びします
ああ! 数時間、この正確な問題が発生しています。 パスは先頭のスラッシュに敏感であることがわかります。
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()
});
最初のスニペットは機能しますが、2番目のスニペットではjson解析エラーが発生します:(
私たちは長い間sendgridの顧客でしたが、APIとこのlibで遊び始めたばかりです。 おそらく、この詳細は十分に文書化されており、私はそれを見落としていますか? スラッシュを省略した場合、libが自動的にスラッシュのプレフィックスを付けると便利です。
@bradseefeld 、
申し訳ありませんが、それに対処する必要がありました:(
私たちは、これらすべての詳細をあなたから隠す、より良いヘルパーに取り組んでいます。 パラメータを気にする必要はありません。
ここでプロジェクトをフォローできます: https :
ありがとう!
心配ない。 私たちはサービスが大好きです!
新しいヘルパーはここで開発中です: https :
最も参考になるコメント
ああ! 数時間、この正確な問題が発生しています。 パスは先頭のスラッシュに敏感であることがわかります。
vs
最初のスニペットは機能しますが、2番目のスニペットではjson解析エラーが発生します:(
私たちは長い間sendgridの顧客でしたが、APIとこのlibで遊び始めたばかりです。 おそらく、この詳細は十分に文書化されており、私はそれを見落としていますか? スラッシュを省略した場合、libが自動的にスラッシュのプレフィックスを付けると便利です。