2016-05-24 7 views
0

私は、メールアドレスをコレクションに追加してsendgridに送信するための流星の方法を持っています。MeteorメソッドがHTTPコールを起動しないときに呼び出す

export const insertEmail = new ValidatedMethod({ 
    name: 'emails.insert', 
    validate: new SimpleSchema({ 
    email: { type: String } 
    }).validator(), 
    run(email) { 
    let isPresent = Emails.find({email: email.email}).count(); 

    var isEmailValid = function(address) { 
     return /^[A-Z0-9'.1234z_%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(address); 
    }; 

    if(isPresent < 1 && isEmailValid(email.email)) { 
     Emails.insert({email: email.email, ip: '1'}); 

     try { 
     HTTP.call('POST', 
      'https://api.sendgrid.com/v3/contactdb/recipients', { 
      headers: { 
       Authorization: "Bearer " + Meteor.settings.sendGridMarketingKey, 
       'Content-Type': 'application/json' 
      }, 
      content: '[{\"email\": \"' + email.email + '\"}]' 
      }, function (error, success) { 
      if (error) { 
       // throw new Meteor.Error(500, error); 
      } else { 
       return success 
      } 
      }); 
     } catch(error) { 
     // throw new Meteor.Error(500, error); 
     } 
    } else { 
     throw new Meteor.Error(500, 'This Email is Already Added!') 
    } 
    }, 
}); 

提出し、電子メールのコレクションに加えては素晴らしい作品が、私は2つの問題を持っている:

それが動作するにもかかわらず

1)、私は私が入力したときに、POSTがhttps://api.sendgrid.com/v3/contactdb/recipients 403(FORBIDDEN)これでも起こり得ますすでに追加されているメールと[このメールは既に追加されています]エラーが表示されます。私のコレクションに電子メールを追加しても対応していないときにPOSTエラーが表示される理由を知っていますか?私は論理を台無しにして、私がしなければならないときにHTTPメソッドを呼び出すと、同時にEmail.insertメソッドをトリガーすることになりますが、HTTPメソッドは独自にトリガーするように思えますemail.emailが存在しない場合でも、それは禁止されたエラーを出力します。

2)誰かのIPアドレスをコレクションに正しく追加する方法がわかりません。私は以前、このコマンドを使用し、それが働いていたが、それはValidateMethodであるため、ここで働いていないようです:

var ipAddress = this.connection.clientAddress; 
check(ipAddress, String); 
+1

オプション引数を抽出してログに記録することができます。私は問題がコンテンツから来ていると思うかもしれません。おそらくjsで実際のオブジェクトを作成し、文字列を連結する代わりに 'JSON.stringify'を作成するようにしてください。 – Ser

+0

ありがとう!私は 'let emailAddress = JSON.stringify(email.email);を使用し、' emailAddress'を使用して残りのポストの値を参照し、それが美しく機能しました! – Coherent

答えて

0

は交換してください:

content: '[{\"email\": \"' + email.email + '\"}]' 

const content = JSON.stringify([{email: email.email}]) 
const options = { 
    headers: {...}, 
    content: content 
} 
[...] 
HTTP.call(type, url, options, function (err, data) {}); 

へ投稿データが正しく送信されるようにしてください。

関連する問題