2016-07-18 7 views
0

を私のコードスニペットは「例外TypeError:オブジェクトは関数ではありません」取得しています。ここエラー

var sendgrid = require('sendgrid')('xxxxxx', 'xxxxxx'); 
var email  = new sendgrid.Email(); 
email.addTo('[email protected]'); 
email.setFrom('[email protected]'); 
email.setSubject('welcome to send grid'); 
email.setHtml('<html><body>HELLO evryone ...,</body></html>'); 
sendgrid.send(email, function(err, json) { 
    if(!err) 
    { 
     console.log("mail sent successssss"); 
     res.send({"status":0,"msg":"failure","result":"Mail sent successfully"}); 
    } 
    else 
    { 
     console.log("error while sending mail") 
     res.send({"status":1,"msg":"failure","result":"Error while sending mail."});  
    } 
}); 

がインストールされている「TypeError例外を取得したNPM also.am throgh sendgrid :オブジェクトは関数 "エラーではありません。私は理由を知っていますか?

バージョン: - [email protected] node_modules \ sendgrid └──[email protected]

答えて

0

あなたが[email protected]を使用しますがsendgridに呼び出ししようとしているように見えます@ 2 * api。

v2の実装:https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/nodejs.html

v3の実装: https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/nodejs.html

がV3行くを与えます。

型エラーに関しては:あなたはV3が

require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY) 

をインストールし、それが今でオブジェクト

だ持っている、しかし、あなたは機能

を起動している

V2

var sendgrid = require("sendgrid")("SENDGRID_APIKEY"); 

要求された更新

私は与えられたキーについてはあまり知りませんが、彼らは別のサポートライブラリのトンを持っているので、それは他の人が一つだけを使用しながら、両方のそれらのいくつかは、使用することを完全に可能です。あなたが本当に唯一USER_API_KEYのNADのPASSWORD_API_KEYを持っている場合は、ちょうどここでuser_api_key

を使用することはnodejs実装モジュールSendGridのための彼らの源である:

function SendGrid (apiKey, host, globalHeaders) { 
    var Client = require('sendgrid-rest').Client 
    var globalRequest = JSON.parse(JSON.stringify(require('sendgrid-rest').emptyRequest)); 
    globalRequest.host = host || "api.sendgrid.com"; 
    globalRequest.headers['Authorization'] = 'Bearer '.concat(apiKey) 
    globalRequest.headers['User-Agent'] = 'sendgrid/' + package_json.version + ';nodejs' 
    globalRequest.headers['Accept'] = 'application/json' 
    if (globalHeaders) { 
    for (var obj in globalHeaders) { 
     for (var key in globalHeaders[obj]) { 
     globalRequest.headers[key] = globalHeaders[obj][key] 
     } 
    } 
    } 

APIKEY認証としてヘッダに取り付けられており、それが見えていますそれがあなたの必要なすべてです。

)オプション()、独自の実装せずに、自分のインストール手順を以下の例更新しますSENDGRID_API_KEYと開発環境、

1を試してみてください:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env 
echo "sendgrid.env" >> .gitignore 
source ./sendgrid.env 

======== 

2)これを作りますあなたが上記を行った場合は、process.env.SENDGRID_API_KEYにUSER_API_KEYを入れてください。

var helper = require('sendgrid').mail 
from_email = new helper.Email("[email protected]") 
to_email = new helper.Email("[email protected]") 
subject = "Hello World from the SendGrid Node.js Library!" 
content = new helper.Content("text/plain", "Hello, Email!") 
mail = new helper.Mail(from_email, subject, to_email, content) 

//process.env.SENDGRID_API_KEY if above is done 
//else just use USER_API_KEY as is 
var sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY) 
var requestBody = mail.toJSON() 
var request = sg.emptyRequest() 
request.method = 'POST' 
request.path = '/v3/mail/send' 
request.body = requestBody 
sg.API(request, function (response) { 
    console.log(response.statusCode) 
    console.log(response.body) 
    console.log(response.headers) 
}) 
+0

....... **ありがとうございます** response.Am nodejsとsendgrid.nmに新しいsendgrid.Butのための2つのAPIキーがあります "SENDGRID_API_KEY"のように述べました.asssume私のキーは 'xxxx'、 'xxxxxxxx' 1つはAPI_USERで、もう1つはAPI-PASSWORDです。私はそれをAPIで書くべきです。明確に私を助けてください。私の仕事はここで中止されました。 @Dreamlines – Trojan

+0

がいっぱい追加されました。基本的にhttps://github.com/sendgrid/sendgrid-nodejsのAPI_USERキーを使用してドキュメントに従ってください。私はあなたのアカウントに行って、このnodejsの実装には単一のAPIキーしか必要ないため、正しいキーを使用していることを確認します。アカウントの設定を確認してください。 API_USER_KEYはまだ正しいかもしれませんが、とにかくチェックしてください。 – Dreamlines

+0

あなたのresponse.iについて心から感謝しています。私のkeys.Butでこのように試しましたが、「エラー」を受け取りました:[{"message": "提供された権限付与が無効、期限切れ、または取り消されました"、 "field"ヘルプ ":null} – Trojan

関連する問題