Meteor Promises in Accounts.onCreateUser

我是研究僧i 提交于 2019-12-10 11:27:38

问题


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!


回答1:


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;
});



回答2:


Yes, you create the promise but you don't wait until it resolves. By the time it does onCreateUser has long returned. Try this:

function createStripeUser(user) {
  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(err);
        }
    });
  })
}

Accounts.onCreateUser(async (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 = {}
  try {
    return await createStripeUser(user);
  } catch(e) {
    // handle your error
  }
});


来源:https://stackoverflow.com/questions/47017782/meteor-promises-in-accounts-oncreateuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!