javascript - promise 不发送任何数据。没有错误吗?

标签 javascript promise stripe.js

当这个 promise 运行时,浏览器就会超时(没有发送任何数据。 ERR_EMPTY_RESPONSE)。终端中没有任何错误。第一个 console.log 被打印,但没有其他内容。

var createStripeCustomer = function(customer, source) {
    console.log('create stripe customer');
    return new Promise(function(res, rej) {
        stripe.customers.create({
            email: customer.email,
            source: source,
            account_balance: 500,
            metadata: {
                first_name: customer.first_name,
                last_name: customer.last_name,
                phone: customer.phone,
                address: `${customer.address} ${customer.appt}`,
                city: customer.city,
                state: customer.state,
                zipcode: customer.zip,
                referral: customer.referral
            },
            function(err, newCustomer) {
                if (err) {
                    console.log(err);
                    rej(err);
                }
                else {
                    console.log('created customer');
                    res(newCustomer);
                }
            }
        });
    });
};

最佳答案

您在 stripe.customers.create 方法第一个参数中错过了 }:

var createStripeCustomer = function(customer, source) {
    console.log('create stripe customer');
    return new Promise(function(res, rej) {
        stripe.customers.create({
            email: customer.email,
            source: source,
            account_balance: 500,
            metadata: {
                first_name: customer.first_name,
                last_name: customer.last_name,
                phone: customer.phone,
                address: `${customer.address} ${customer.appt}`,
                city: customer.city,
                state: customer.state,
                zipcode: customer.zip,
                referral: customer.referral
             }
            },
            function(err, newCustomer) {
                if (err) {
                    console.log(err);
                    rej(err);
                }
                else {
                    console.log('created customer');
                    res(newCustomer);
                }
            }
        });
    });
};

然后它应该以这种方式工作:

createStripeCustomer(customer, source).then( res => console.log(res) ).catch(error => console.error(error))

关于javascript - promise 不发送任何数据。没有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562881/

相关文章:

node.js - Bluebird promise, promise.each 只执行一次

javascript - 带选项的 Stripe 自定义结帐

javascript - React.js onclick事件: Cannot read property 'props' of undefined

javascript - 使用javascript替换字符串中的 "f(x)="、 "g(x)="→ "*(x)"

javascript - 如何在拒绝时破坏 Bluebird Promise.app()?

jquery - 延期与 promise

javascript - 如何将本地自定义字体应用于 Stripe Elements?

ios - 使用 Stripe.js (iOS) 为 Stripe Connect 支付创建 external_account

javascript - 如何减少Document.getelementbyid

JavaScript 运行时错误 : '$' is undefined