解析云代码Mandrill承诺

时间:2015-03-17 16:03:06

标签: javascript parse-platform promise mandrill cloud-code

所以我遇到了从Mailgun迁移并开始使用Mandrill的问题。我遵循Parse Purchase应用程序教程,并且具有非常相似的代码库。这是目前的成功运作。

return Mailgun.sendEmail({
    to: currentUser.get('email'),
    from: hostEmail,
    subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
    text: body
}).then(null, function(error) {
    return Parse.Promise.error('Your purchase was successful, but we were not able to send you an email.');
});

因此成功运行,不会引发任何错误。

所以继续相当于Mandrill,

return Mandrill.sendEmail({
    message: {
        text: body,
        subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
        from_email: hostEmail,
        from_name: appname,
        to: [{
            email: currentUser.get('email'),
            name: currentUser.get('displayName')
        }]
    },
    async: true
}).then(null, function(error) {
    console.log('Sending email failed. Error: ' + error);
    return Parse.Promise.error('Your purchase was successful, but we were not able to send you an email.');
});

显然,这不起作用。

错误日志显示:

Error: TypeError: Cannot read property 'success' of undefined
at Object.exports.sendEmail (mandrill.js:55:21)
at main.js:115:25
at e (Parse.js:2:6670)
at Parse.js:2:6119
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)
at c.extend.resolve (Parse.js:2:6070)
at Parse.js:2:6749
at e (Parse.js:2:6670)
at Parse.js:2:6119 (Code: 141, Version: 1.6.0)

所以我认为Mandrill成功发送了电子邮件,因为它搜索了'success'属性,但Promise总是失败并将错误响应返回给iOS应用程序。

任何帮助将不胜感激!

再次感谢

1 个答案:

答案 0 :(得分:1)

发现问题。显然你必须实际声明一个promise变量并向它发送成功/失败回调。

我在评论中发布的要点链接帮助了我gist Link

这是我现在所做的事情,

Parse.Cloud.define('purchase', function(request, response) { 
    ...

    ...

    ...

    var message = {
            text: body,
            subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
            from_email: hostEmail,
            from_name: appname,
            to: [{
                email: currentUser.get('email'),
                name: currentUser.get('displayName'),
            }]
        };

    return sendMandrillEmailPromise(message).then(null, function(error) {
        console.log('Sending email failed. Error: ' + error);
        return Parse.Promise.error('Your purchase was successful, but we not able to send you an email');
    ...

    ...

    ...  
});

var sendMandrillEmailPromise = function(message){
    var promise = new Parse.Promise();
    Mandrill.sendEmail({
        message: message,
        async: true,
    },{
        success: function(httpResponse) {
            promise.resolve(httpResponse);
        },
        error: function(error) {
            promise.error(error);
        }
    });
    return promise;
}

按预期工作,我收到了一封很棒的电子邮件!谢谢大家的意见!