1

I'm using vpulim:node-soap to have a soap server running.

My meteor server startup contains this amongst various other code:

  authRequestOperation: function(args,cb,headers,req) {
      console.log(args);
      var authResponceObject = {};
      var futureAuthResponse = new Future();
      Fiber(function(){
        if(collectorUsers.findOne({username: args.username})){
          console.log("Found User");
          authResponceObject = {
            username: args.username,
            nonce: Random.id()
          };
          console.log("authResponceObject is: " + JSON.stringify(authResponceObject,null,4));
          console.log("futureAuthResponse returning...");
          futureAuthResponse.return(authResponceObject);
        }
        // console.log("futureAuthResponse waiting...");
        // return futureAuthResponse.wait();


      }).run();
      console.log("authResponceObject after fiber is: " + JSON.stringify(authResponceObject,null,4));
      return authResponceObject;
  },

What I'm trying to do is:

  1. I receive a user object from client.
  2. I check if the user is present in the mongodb
  3. If user is present, prepare response object
  4. Respond to the client with the response object.

I have 1. working. However, the dute it being async call, the order of 2,3,4 is messed up.

Right now what's happening is:

  1. receive client object
  2. return response object (which is empty)
  3. Check mongo
  4. Prepare response object.

I'm not using Meteor.methods for the above. How do I make this work in the right manner? I've tried juggling around wrapAsync and fiber/future but hitting dead ends.

blueren
  • 2,730
  • 4
  • 30
  • 47
  • Why do you use `Fiber` in your code above? `.findOne` is already sync. Do you see error if not using `Fiber`? – kkkkkkk Nov 29 '16 at 14:49
  • Since i'm running the code outside of Meteor.methods, I need to wrap it in a Fiber. Or else I get an error saying that Meteor need to run with a fiber. – blueren Nov 29 '16 at 16:21

1 Answers1

1

I believe Meteor.bindEnvironment can solve your problem, try this code:

{
  // ...
  authRequestOperation: Meteor.bindEnvironment(function(args, cb, headers, req) {
    console.log(args);
    var authResponceObject = {};

    if (collectorUsers.findOne({username: args.username})) {
      console.log("Found User");
      authResponceObject = {
        username: args.username,
        nonce: Random.id()
      };
      console.log("authResponceObject is: " + JSON.stringify(authResponceObject, null, 4));
    }


    return authResponceObject;
  }),
  // ...
}
kkkkkkk
  • 7,628
  • 2
  • 18
  • 31