解析CC - 成功回调未触发

时间:2015-07-11 20:53:53

标签: javascript parse-platform cloud-code

我创建了一个用于创建新Stripe客户的Cloud Code功能,该功能正常。我无法弄清楚的是将客户ID返回给调用代码;

致电代码

Parse.Cloud.run(
    'createCustomer',
    {
        email: user.get('email'),
        username: user.get('username')
    },
    {
        success: function(customerId) {
            // never called
            console.log('Customer created: ' + customerId);
        },
        error: function() {
            console.log('Error function');
        }
    }
);

功能

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

        var email = request.params.email,
            description = 'Username: ' + request.params.username;

        Parse.Config.get().then(function(config) {

            var Stripe = require('stripe');
            Stripe.initialize(config.get('stripe_secret_test'));

            Stripe.Customers.create({
                description: description,
                email: email
            })
            .then(function(customer) {
                console.log(customer.id); // Logs out fine
                response.success(customer.id);
            });
        });
    }
);

基本上,调用代码中的成功回调永远不会被触发,因此我无法访问我想要返回的客户ID。我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

如果从Cloud Code中调用云函数似乎不能很好地运行它们,它们会运行,但我找不到返回值的方法。

相反,我使用了Cloud Code Module,因为只需要从Cloud Code中调用此函数。最终代码如下;

调用代码(User beforeSave)

var createCustomerModule = require('cloud/stripe/CreateCustomer.js');

createCustomerModule.createCustomer(
    user.get('email'),
    user.get('username'),
    {
        success: function(customer) {
            request.object.set('stripeCustomerId', customer.id);
            response.success();
        },
        error: function() {
            response.error('Failed to create a new Stripe customer');
        }
    }
);

功能(cloud / stripe / CreateCustomer.js)

exports.createCustomer = function(email, username, callbacks) {

    var description = 'Username: ' + username;

    Parse.Config.get().then(function(config) {

        var Stripe = require('stripe');
        Stripe.initialize(config.get('stripe_secret_test'));

        // Attempt to create the customer
        Stripe.Customers.create({
            description: description,
            email: email
        })
        .then(callbacks.success, callbacks.error);
    });
};