Search code examples
javascriptnode.jsmeteorsoapnode-soap

Async functions in meteor server


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.


Solution

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