2016-01-05 31 views
-1

我爲我的Angular 2 Typescript應用程序使用npm install sendgrid。使用Send Grid和我的Angular2 Typescript應用程序時的參考錯誤

我已經安裝sendgrid和包,但我不斷收到一個控制檯錯誤:

「的ReferenceError:要求沒有定義」

這是我的代碼示例(我已經刪除了原因很明顯鍵):)

var sendgrid_api_key = "key goes here"; 

    var sendgrid = require('sendgrid')(sendgrid_api_key); 

    sendgrid.send({ 
    to:  '[email protected]', 
    from:  '[email protected]', 
    subject: 'Hello Mark Test', 
    text:  'My first email through SendGrid.' 
    }, function(err, json) { 
    if (err) { return console.error(err); } 
     console.log(json); 
    }); 
+0

這與Angular有什麼關係? –

+0

我在我的角度2項目中使用發送網格,你知道如何讓發送網格在這個環境中工作嗎? – AngularM

回答

1

您不能從客戶端代碼使用SendGrid。這本質上是不安全的,因爲任何人都可以查看代碼並獲得您的憑據。

1

要求(僅服務器端(即節點),它沒有客戶端/瀏覽器的JavaScript存在。確保您通過SystemJS或Webpack運行您的Angular2應用程序。

+0

你好, 對於我的應用程序,我沒有服務器端。我正在使用SystemJS。 是否有可能只發送網格工作客戶端? – AngularM

+0

這是我從那裏得到的例子:https://www.npmjs.com/package/sendgrid - 但我沒有節點js。任何其他想法? – AngularM

+0

會這樣認爲。嘗試從'sendgrid'導入*作爲sendgrid,並將sendgrid js依賴項包含在您的初始html中。 –

相關問題