Accounts.onCreateUser 中的流星承诺 [英] Meteor Promises in Accounts.onCreateUser
问题描述
我想在用户注册期间创建一个条纹帐户,并为此目的调整 Accounts.onCreateUser 并承诺.
Accounts.onCreateUser((options, user) => {如果(user.services.facebook){const { first_name, last_name, email } = user.services.facebook;用户配置文件 = {}user.profile.first_name = first_nameuser.profile.last_name = last_name}别的{user.profile = options.profile}user.stripe = {}return new Promise((resolve,reject) => {stripe.customers.create({描述:user.profile.first_name + ' ' + user.profile.last_name},函数(错误,响应){如果(!错误){user.stripe.id = response.id解决(用户);} 别的 {拒绝('无法创建用户');}});})});
虽然在条带中正确创建了用户,但meteor mongo 数据库中的用户文档仅包含用户 ID,没有其他字段.
我是否使用了错误的承诺?任何帮助将不胜感激!
因为 onCreateUser
在服务器上运行,我们可以使用 Meteor.wrapAsync
将 Stripe 调用包装在 Fiber 中>.
Fibers 允许异步代码像同步一样运行,但只能在服务器上运行.(这里有一个关于 Fiber 是什么以及 Meteor 为何使用它们的精彩演示)>
使用 wrapAsync
代码如下所示:
Accounts.onCreateUser((options, user) => {如果(user.services.facebook){const { first_name, last_name, email } = user.services.facebook;用户配置文件 = {}user.profile.first_name = first_nameuser.profile.last_name = last_name} 别的 {user.profile = options.profile}user.stripe = {};const createStripeCustomer = Meteor.wrapAsync(stripe.customers.create,stripe.customers);const 响应 = createStripeCustomer({描述:user.profile.first_name + ' ' + user.profile.last_name});user.stripe.id = response.id返回用户;});
I would like to create a stripe account during the user registration in meteor and adjusted Accounts.onCreateUser for that purpose with a promise.
Accounts.onCreateUser((options, user) => {
if (user.services.facebook) {
const { first_name, last_name, email } = user.services.facebook;
user.profile = {}
user.profile.first_name = first_name
user.profile.last_name = last_name
}
else{
user.profile = options.profile
}
user.stripe = {}
return new Promise((resolve,reject) => {
stripe.customers.create({
description: user.profile.first_name + ' ' + user.profile.last_name
},function(err,response){
if (!err) {
user.stripe.id = response.id
resolve(user);
} else {
reject('Could not create user');
}
});
})
});
While the user gets properly created in stripe, the user document in the meteor mongo database only contains the userid but no other field.
Am I using the promise wrong? Any help would be appreciated!
Because onCreateUser
runs on the server, we can wrap the Stripe call in a Fiber using Meteor.wrapAsync
.
Fibers allow async code to run as though it was synchronous, but only on the server. (Here's a great presentation on what Fibers are and why Meteor uses them)
With wrapAsync
the code looks like this:
Accounts.onCreateUser((options, user) => {
if (user.services.facebook) {
const { first_name, last_name, email } = user.services.facebook;
user.profile = {}
user.profile.first_name = first_name
user.profile.last_name = last_name
} else {
user.profile = options.profile
}
user.stripe = {};
const createStripeCustomer = Meteor.wrapAsync(stripe.customers.create,stripe.customers);
const response = createStripeCustomer({
description: user.profile.first_name + ' ' + user.profile.last_name
});
user.stripe.id = response.id
return user;
});
这篇关于Accounts.onCreateUser 中的流星承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!