承诺不发送任何数据。没有错误?
Promise not sending any data. No error?
当这个承诺 运行 时,浏览器就会超时(没有发送任何数据。
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))
当这个承诺 运行 时,浏览器就会超时(没有发送任何数据。 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))