如何存储标准输出的结果?
How to store the result of a stdout?
我执行一个 UNIX 命令来列出我所有以 .svg
结尾的文件,就像这样
'getExistingFiles': function () {
var list ="";
child = exec_tool('cd /home/me/files/; ls *.svg',
function (error, stdout, stderr) {
list = stdout;
console.log(typeof list);
console.log("LIST:------------");
console.log(list);
return list;
if (error !== null) {
console.log('exec error: ' + error);
list = "error: " + error;
return list;
}else{
console.log("Listing done");
}
});
}
我有一个结果:
string
LIST:------------
test.svg
output.svg
test2.svg
然后 JavaScript 我想为 list
中的每个文件创建一个新元素,但我不能 return 我的 list
我总是“未定义”
那我的 list
怎么了?为什么我不能从客户端访问它,尽管它是一个字符串?我认为错误出在服务器上,您能帮我找到它吗?
因为exec_tool是异步函数?
尝试wrapAsync, somethign like this. See more from docs。
'getExistingFiles': function () {
var list ="";
var et = Meteor.wrapAsync(exec_tool);
try {
child = et('cd /home/me/files/; ls *.svg');
return child.stdout;
} catch (err) {
throw new Meteor.Error(err, err.stack);
}
}
看看这是否适合您。使用 fiber/future 的异步函数。让我们调整一下,以防您 运行 遇到问题。
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);
});
我执行一个 UNIX 命令来列出我所有以 .svg
结尾的文件,就像这样
'getExistingFiles': function () {
var list ="";
child = exec_tool('cd /home/me/files/; ls *.svg',
function (error, stdout, stderr) {
list = stdout;
console.log(typeof list);
console.log("LIST:------------");
console.log(list);
return list;
if (error !== null) {
console.log('exec error: ' + error);
list = "error: " + error;
return list;
}else{
console.log("Listing done");
}
});
}
我有一个结果:
string
LIST:------------
test.svg
output.svg
test2.svg
然后 JavaScript 我想为 list
中的每个文件创建一个新元素,但我不能 return 我的 list
我总是“未定义”
那我的 list
怎么了?为什么我不能从客户端访问它,尽管它是一个字符串?我认为错误出在服务器上,您能帮我找到它吗?
因为exec_tool是异步函数?
尝试wrapAsync, somethign like this. See more from docs。
'getExistingFiles': function () {
var list ="";
var et = Meteor.wrapAsync(exec_tool);
try {
child = et('cd /home/me/files/; ls *.svg');
return child.stdout;
} catch (err) {
throw new Meteor.Error(err, err.stack);
}
}
看看这是否适合您。使用 fiber/future 的异步函数。让我们调整一下,以防您 运行 遇到问题。
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);
});