Meteor Wrapasync服务器端将api调用结果保存到集合中

时间:2016-06-29 18:37:52

标签: javascript meteor

我试图在Accounts.onCreateUser回调中的异步api调用之后在集合中插入一些数据 (API:https://github.com/Mangopay/mangopay2-nodejs-sdk)。

但是我收到了错误

throw new Error("Meteor code must always run within a Fiber. " +
Error: Meteor code must always run within a Fiber. Try wrapping  
callbacks that you pass to non-Meteor libraries with 
Meteor.bindEnvironment.

这是我的第一次尝试:     Accounts.onCreateUser(function(options,user){

    mangoPayApi.Users.create({
    "Email": options.email,
    "FirstName": options.profile.firstName,
    "LastName": options.profile.lastName,
    "CountryOfResidence": "FR",
    "Nationality": "FR",
    "Birthday": new Date(aBirthDateTimestamp).getTime() / 1000,
    "PersonType": "NATURAL",
    "Tag": user._id,
    }, function(mpUser) {
        // User created - using callback
        console.log('User created ');
        console.log(mpUser);
        aMPData.user = mpUser;
        MPData.insert(aMPData); // Insert data into collection

第二枪: 我试图让api调用异步

     let synCreateUser = Meteor.wrapAsync(mangoPayApi.Users.create, mangoPayApi.Users );
     user = synCreateUser.create({
    "Email": post.emails[0],
    "FirstName": post.profile.firstName,
    "LastName": post.profile.lastName,
    "CountryOfResidence": "FR",
    "Nationality": "FR",
    "Birthday": new Date(aBirthDateTimestamp).getTime() / 1000,
    "PersonType": "NATURAL",
    "Tag": post._id,
  });

但现在我收到以下错误

Exception in queued task: TypeError: Object function (/* arguments */) {
   var self = context || this;
   var newArgs = _.toArray(arguments);
   var callback;
   for (var i = newArgs.length - 1; i >= 0; --i) {
     var arg = newArgs[i];
     var type = typeof arg;
     if (type !== "undefined") {
       if (type === "function") {
         callback = arg;
       }
       break;
     }
   }

   if (! callback) {
     if (Meteor.isClient) {
       callback = logErr;
     } else {
       var fut = new Future();
       callback = fut.resolver();
     }
     ++i; // Insert the callback just after arg.
   }

   newArgs[i] = Meteor.bindEnvironment(callback);
   var result = fn.apply(self, newArgs);
   return fut ? fut.wait() : result;
 } has no method 'create'
 at Object.added (server/main.js:102:30)
 at [object Object].observeChangesCallbacks.added (packages/minimongo/observe.js:153:1)
 at self.applyChange.added (packages/minimongo/observe.js:53:1)

如何将从api调用中获取的数据插入到集合中?

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定handleCharge是什么,但如果你想使用你在上一行创建的同步功能,基本上应该调用synCreateUser

let synCreateUser = Meteor.wrapAsync(mangoPayApi.Users.create, mangoPayApi.Users);
user = synCreateUser({
  "Email": post.emails[0],
  "FirstName": post.profile.firstName,
  "LastName": post.profile.lastName,
  "CountryOfResidence": "FR",
  "Nationality": "FR",
  "Birthday": new Date(aBirthDateTimestamp).getTime() / 1000,
  "PersonType": "NATURAL",
  "Tag": post._id,
});

// now you can process result from user variable

答案 1 :(得分:0)

/* Create MangoPay user */
  mangoPayApi.Users.create({
    "Email": document.emails[0].address,
    "FirstName": document.profile.firstName,
    "LastName": document.profile.lastName,
    "CountryOfResidence": "FR",
    "Nationality": "FR",
    "Birthday": new Date(aBirthDateTimestamp).getTime() / 1000,
    "PersonType": "NATURAL",
    "Tag": document._id,
  }, Meteor.bindEnvironment( function(mpUser) {

    // User created - using callback
    console.log('User created ');
    console.log(mpUser);

    /* Mangopay Id */
    aMPData.Id = mpUser.Id;

    if(mpUser)
    {
      mangoPayApi.Wallets.create({
        "Owners": [mpUser.Id],
        "Description": "Client Wallet",
        "Currency": "EUR"
      }, Meteor.bindEnvironment( function(clientWallet) {
        console.log('wallet created');
        console.log(clientWallet);

        aMPData.clientWallet.Id = clientWallet.Id;
        aMPData.clientWallet.Owner = clientWallet.Owners[0];

        /* MangoPay clientWallet wallet created */
        if(clientWallet)
        {
          mangoPayApi.Wallets.create({
            "Owners": [clientWallet.Owners[0]],
            "Description": "mw Wallet",
            "Currency": "EUR"
          }, Meteor.bindEnvironment(function(mw) {
            if(mw)
            {
              console.log(mw);
              aMPData.mw.Id = mw.Id;
              aMPData.mw.Owner = mw.Owners[0];
              // Mangopay.insert(aMPData);
              Meteor.users.update(document._id, { $set: { mangopay: aMPData } });
            }
          }));
        }
      })); // callback mangoPayApi.Wallets  // Meteor.bindEnvironment callback angoPayApi.Wallets  // mangoPayApi.Wallets.create
    } // end if mpUser
  })); // callback Users.create // Meteor.bindEnvironment callback Users.create// mangoPayApi.Users.create;
}