解析云代码 Mandrill Promise

Parse Cloud Code Mandrill Promise

所以我在从 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 总是失败并且 returns 错误响应返回给 iOS 应用程序。

任何帮助将不胜感激!

再次感谢

找到问题了。显然,您必须实际声明一个 promise 变量并将其发送给 success/failure 回调。

我在评论中发布的要点link对我有帮助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;
}

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