parse.com 云代码无法通过 mandrillapp 发送电子邮件

Posted

技术标签:

【中文标题】parse.com 云代码无法通过 mandrillapp 发送电子邮件【英文标题】:parse.com cloud code can't send email via mandrillapp 【发布时间】:2014-07-30 13:55:35 【问题描述】:

我正在尝试通过 parse.com 通过 mandrill 发送电子邮件。这些示例很容易理解,但我遇到了一个奇怪的错误。

这是我的代码:

  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: "parse@cloudcode.com",
          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 = 'myemail@bla.com';
IPLogger_config.your_name = 'myName';

mandrillAppKey 设置正确。我仔细检查了那个。从 mandrill 网站发送电子邮件也可以。我刚刚创建了一个新帐户,并没有在山魈网站上进行其他设置。

我收到此错误:"code":-1,"name":"ValidationError","message":"You must specify a key value"。在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":

问题可能出在哪里?任何想法如何让它运行?

非常感谢!

【问题讨论】:

给定 'var IPLogger_config = require('cloud/mandrillapp_config.js');' mandrillAppKey 是否已导出 -- 'exports.mandrillAppKey' ? 嗨,我用我的配置脚本的内容更新了这个问题。看起来 mandrillAppKey 已在配置中设置,但在主脚本中可用... 【参考方案1】:

我终于找到了问题所在。配置脚本中存在错误。

它需要看起来像这样:

var IPLogger_config = ;
IPLogger_config.mandrillAppKey = "xxx";
IPLogger_config.your_email = 'myemail@bla.com';
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);

现在它工作正常。

【讨论】:

以上是关于parse.com 云代码无法通过 mandrillapp 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

Mandrill Handlebar 模板无法正确渲染/与 Parse.com 对象合并

Android、Mandrill 和 Parse.com:Java ClassCastException:HashMap 无法转换为类

将 Java 字符串数组或 JSON 字符串转换为 Javascript 数组(Parse.com Cloud Code 和 Mandrill)

mc:edit 在带有 Mandrill Javascript API 的 Mailchimp 模板中不起作用

Parse.com 云代码 - 无法弄清楚为啥 javascript 变量在所有数组项中具有相同的值

Parse.com SendGrid 云模块:无法发送标头参数对象