【问题标题】:How to return a sdtout from a server to a client inside a string?如何在字符串中将 sdtout 从服务器返回到客户端?
【发布时间】:2017-10-21 23:14:17
【问题描述】:

我正在尝试返回我的方法的stdout,但在客户端我总是有undefined,尽管服务器说它是带有内容的string

我这样做:

'getExistingFiles': function () {
      var list = "";
      return new Promise(Meteor.bindEnvironment(function(resolve) {
        child = exec_tool("ls -al",
          function (error, stdout, stderr) {
            if (error !== null) {
              console.error('exec error: ' + error);
              list = "error: " + error;
              resolve(list);
              return list;
            }else{
              list = stdout;
              console.log(typeof list);
              console.log("LIST:------------");
              console.log(list);
              resolve(list);
              return list;
            }
          });
      }));
}

在我的终端上,我有日志:

但在客户端上,当我尝试访问它的值时,它是undefined

Meteor.call("getExistingFiles",function(list){
       console.log(list);
       console.log(typeof list);
});

所以我的问题是,我如何将这个list 发送给客户?

[编辑] 我试过这样来测试是不是我的客户错了,但不是我认为是在服务器上

//server      
var et = Meteor.wrapAsync(exec_tool);
 try {
  child = et('ls -al');
  console.log("LIST:------------");
  console.log(child);
  console.log(typeof child);
  return child;
 } catch (err) {
  throw new Meteor.Error(err, err.stack);
}

[EDIT2] 即使这样它也会发送undefined WHY ?!

var et = Meteor.wrapAsync(exec_tool);
      try {
       var proc = exec_tool("ls -al")
       proc.stdout.on('data', function (data) {
         //do something with data
         console.log(data);
         list = list + data;
       });
       proc.stdout.on('end', function() {
         console.log("end");
         console.log(list);
         return list;
       });
      } catch (err) {
       throw new Meteor.Error(err, err.stack);
      }

【问题讨论】:

    标签: javascript string meteor promise client-server


    【解决方案1】:

    在服务器上(来自服务器的承诺被评估,然后在完成后发送给客户端):

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

    在客户端:

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

    Promise 没有return。来自异步函数的回调通常具有 function (error, result) 参数列表。因此,想要的结果应该在第二个参数中。试试这样。

    【讨论】:

    【解决方案2】:

    看起来这是this的欺骗问题

    看看这是否适合你。使用光纤/未来的异步功能。让我们调整一下,以防遇到问题。

    Server.js

      // 
      // Load future from fibers
      var Future = Npm.require("fibers/future");
      // Load exec
      var 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=new Future();
          var command="cd /home/me/files/; ls *.svg";
          exec(command,function(error,stdout,stderr){
            if(error){
              console.log(error);
              throw new Meteor.Error(500,command+" failed");
            }
            future.return(stdout.toString());
          });
          return future.wait();
        }
      });
    

    Client.js:

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

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      相关资源
      最近更新 更多