2014-06-09 272 views
0

我試圖通過parse.com通過mandrill發送電子郵件。這些示例很容易遵循,但我遇到了一個奇怪的錯誤。parse.com雲代碼無法通過mandrillapp發送電子郵件

這裏是我的代碼:

Parse.Cloud.afterSave("ip", function(request) { 
    var IPLogger_config = require('cloud/mandrillapp_config.js'); 
    var Mandrill = require('mandrill'); 
    Mandrill.initialize(IPLogger_config.mandrillAppKey); 

    console.log('within afterSave for ip'); 
    console.log(request.object.id); 


    var ip = Parse.Object.extend("ip"); 
    var query = new Parse.Query(ip); 
    query.descending("createdAt"); 
    query.limit(2); // limit to at most 2 results 
    query.find({ 
    success: function(results){ 
     console.log('success query'); 
     console.log('got ' + results.length + ' results'); 
     var newestIp = results[0]; 
     var olderIp = results[1]; 
     if (newestIp.get('ip') == olderIp.get('ip')) { 
     // the newest ip and the older one are equal, do nothing. 
     console.log('No ip change'); 
     } else 
     { 
     console.log('ip change!'); 
     console.log(Mandrill.initialize); 
     console.log(Mandrill.sendEmail); 

     Mandrill.sendEmail({ 
     message: { 
      text: "The IP of your server has changed! The new ip is: " + newestIp.get('ip') , 
      subject: "The IP of your server has changed!", 
      from_email: "[email protected]", 
      from_name: "IPLogger", 
      to: [ 
      { 
       email: IPLogger_config.your_email, 
       name: IPLogger_config.your_name 
      } 
      ] 
     }, 
     async: true 
     },{ 
     success: function(httpResponse) { 
      console.log(httpResponse); 
      response.success("Email sent!"); 
     }, 
     error: function(httpResponse) { 
      console.error(httpResponse); 
      response.error("Uh oh, something went wrong"); 
     } 
     }); 




     } 
    }, 
    error: function (error){ 
     console.log('no success for query'); 
     console.error("Got an error " + error.code + " : " + error.message); 
    } 

    }); 
}); 

我mandrillapp_config.js看起來是這樣的:

var IPLogger_config = {}; 
IPLogger_config.mandrillAppKey = "xxx"; 
IPLogger_config.your_email = '[email protected]'; 
IPLogger_config.your_name = 'myName'; 

mandrillAppKey設置正確。我仔細檢查了一下。從mandrill網站發送電子郵件也可以。我剛剛創建了一個新帳戶,並沒有在mandrill站點上進行其他設置。我收到此錯誤:。在https://www.parse.com/questions/sometimes-getting-mandrill-you-must-specify-a-key-value-error-when-sending-email被寫爲頭可能是錯誤的,但正如你可以看到我的日誌,"Content-Type":"application/json; charset=utf-8"設置正確。

I2014-06-09T22:34:20.601Z] { 
    "uuid":"fbb215c4-1d2a-e2da-23fc-a838bd6bf217", 
    "status":500, 
    "headers":{ 
     "Access-Control-Allow-Credentials":"false", 
     "Access-Control-Allow-Headers":"Content-Type", 
     "Access-Control-Allow-Methods":"POST, GET, OPTIONS", 
     "Access-Control-Allow-Origin":"*", 
     "Connection":"close", 
     "Content-Encoding":"gzip", 
     "Content-Type":"application/json; charset=utf-8", 
     "Date":"Mon, 09 Jun 2014 22:34:20 GMT", 
     "Server":"nginx/1.6.0", 
     "Vary":"Accept-Encoding", 
     "X-Powered-By":"PHP/5.3.10-1ubuntu3.11" 
    }, 
    "text":"{\"status\":\"error\",\"code\":-1,\"name\":\"ValidationError\",\"message\":\"You must specify a key value\"}", 
    "data":{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}, 
    "buffer":{"0":123,"1":34,"2":115,"3":116,"4":97,"5":116,"6":117,"7":115,"8":34,"9":58,"10":34,"11":101,"12":114,"13":114,"14":111,"15":114,"16":34,"17":44,"18":34,"19":99,"20":111,"21":100,"22":101,"23":34,"24":58,"25":45,"26":49,"27":44,"28":34,"29":110,"30":97,"31":109,"32":101,"33":34,"34":58,"35":34,"36":86,"37":97,"38":108,"39":105,"40":100,"41":97,"42":116,"43":105,"44":111,"45":110,"46":69,"47":114,"48":114,"49":111,"50":114,"51":34,"52":44,"53":34,"54":109,"55":101,"56":115,"57":115,"58":97,"59":103,"60":101,"61":34,"62":58,"63":34,"64":89,"65":111,"66":117,"67":32,"68":109,"69":117,"70":115,"71":116,"72":32,"73":115,"74":112,"75":101,"76":99,"77":105,"78":102,"79":121,"80":32,"81":97,"82":32,"83":107,"84":101,"85":121,"86":32,"87":118,"88":97,"89":108,"90":117,"91":101,"92":34,"93":125,"length":94, 
    "parent":{"0":123,"1":34,"2":115,"3":116,"4":97,"5":116,"6":117,"7":115,"8":34,"9":58,"10":34,"11":101,"12":114,"13":114,"14":111,"15":114,"16":34,"17":44,"18":34,"19":99,"20":111,"21":100,"22":101,"23":34,"24":58,"25":45,"26":49,"27":44,"28":34,"29":110,"30":97,"31":109,"32":101,"33":34,"34":58,"35":34,"36":86,"37":97,"38":108,"39":105,"40":100,"41":97,"42":116,"43":105,"44":111,"45":110,"46":69,"47":114,"48":114,"49":111,"50":114,"51":34,"52":44,"53":34,"54":109,"55":101,"56":115,"57":115,"58":97,"59":103,"60":101,"61":34,"62":58,"63":34,"64":89,"65":111,"66":117,"67":32,"68":109,"69":117,"70":115,"71":116,"72":32,"73":115,"74":112,"75":101,"76":99,"77":105,"78":102,"79":121,"80":32,"81":97,"82":32,"83":107,"84":101,"85":121,"86":32,"87":118,"88":97,"89":108,"90":117,"91":101,"92":34,"93":125,"length":94},"offset":0},"cookies":{}} 

哪裏可以解決問題?任何想法如何讓它運行?

非常感謝你!

+0

鑑於'var IPLogger_config = require('cloud/mandrillapp_config.js');'是mandrillAppKey導出 - 'exports.mandrillAppKey'? – kingdango

+0

嗨,我更新了我的配置腳本的內容的問題。看起來mandrillAppKey在配置中被設置,但在主腳本中可用... – otmezger

回答

0

我終於找到了問題。配置腳本中有錯誤。

需要它看起來像這樣:

var IPLogger_config = {}; 
IPLogger_config.mandrillAppKey = "xxx"; 
IPLogger_config.your_email = '[email protected]'; 
IPLogger_config.your_name = 'myName'; 
exports.IPLogger_config = IPLogger_config; 
main.js文件

,需要在配置文件的資源,被稱爲是這樣的:

var config = require('cloud/mandrillapp_config.js'); 
Mandrill.initialize(config.IPLogger_config.mandrillAppKey); 

,現在它工作正常。

相關問題