Skip to content Skip to sidebar Skip to footer

How To Return A Sdtout From A Server To A Client Inside A String?

I'm trying to return the stdout of my method but on the client I always have undefined despite the server says it's a string with content. I do that: 'getExistingFiles': function (

Solution 1:

On the server (promises from the server are evaluated and then sent to the client when they're done):

getExistingFiles: function () {
      returnnewPromise((resolve, reject) => {
        child = exec_tool("ls -al",
          function (error, stdout, stderr) {
            if (error) {
              reject(error);
            } else {
              resolve(stdout);
            }
          });
      }));
}

And on the client:

Meteor.call("getExistingFiles", function(err, list) {
    if(err) {
        // handle your errorthrow err;
    }
    console.log(list);
});

Promises don't have return. Callbacks from async functions usually have the function (error, result) argument list. So, the wanted result should be in the second argument. Try it like this.

Solution 2:

Looks like this is a dupe question of this

See if this works for you. Async function using fiber/future. Let's tweak this in case you run into issues.

Server.js

// // Load future from fibersvarFuture = Npm.require("fibers/future");
  // Load execvar exec = Npm.require("child_process").exec;

  Meteor.methods({
    runListCommand: function () {
      // This method call won't return immediately, it will wait for the// asynchronous code to finish, so we call unblock to allow this client// to queue other method calls (see Meteor docs)this.unblock();
      var future=newFuture();
      var command="cd /home/me/files/; ls *.svg";
      exec(command,function(error,stdout,stderr){
        if(error){
          console.log(error);
          thrownewMeteor.Error(500,command+" failed");
        }
        future.return(stdout.toString());
      });
      return future.wait();
    }
  });

Client.js:

Meteor.call('runListCommand', function (err, response) {
  console.log(response);
});

Post a Comment for "How To Return A Sdtout From A Server To A Client Inside A String?"