hlx hlx - 3 months ago 13
Javascript Question

Meteor Wrapasync server side save api call result to collection

I am trying to insert some data in a collection after an asynchronous api call in the Accounts.onCreateUser callback
(API: https://github.com/Mangopay/mangopay2-nodejs-sdk).

However I am getting the error

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.


Here's my first try:
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


Second shot:
I tried to make the api call aynchronous

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


But now I get the following error

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)


How can I insert the data I get from an api call into a collection ?

Thanks !

hlx hlx
Answer
/* 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;
}