2014-02-27 47 views
1

我嘗試從Meteor環境中的child_process命令獲取結果。 似乎有什麼特別的child_process,我不明白。 這裏是我用來測試Meteor._wrapAsync和節點子進程

Meteor.startup(function() { 
    exec = Npm.require('child_process').exec; 
}); 

function bind_environment_callback(error) { 
    console.log('Error binding environment for a callback', error.stack); 
} 

function get_git_commit_hash(cb) { 
    exec( 
     'git rev-parse HEAD', 
     Meteor.bindEnvironment(
      function(error, stdout, stderr) { 
       if (error) { 
        cb('Error retrieving commit hash', null); 
       } else { 
        console.log("Inside get_git_commit_hash:" + stdout.slice(0,stdout-1).toString()); 
        cb(null, stdout.slice(0,stdout-1).toString()); 
       } 
      }, 
      bind_environment_callback 
     ) 
    ); 
} 

function dummy(cb){ 
    setTimeout(function(){ 
     cb(null, 'Dummy result'); 
    }, 
    100); 
} 

Meteor.methods({ 
    test: function() { 
     var get_git_commit_hash_sync = Meteor._wrapAsync(get_git_commit_hash); 
     var result= get_git_commit_hash_sync(); 
     console.log('Call 1:' + result); 

     var dummy_sync = Meteor._wrapAsync(dummy); 
     result= dummy_sync(); 
     console.log('Call 2:' + result); 
    } 
}); 

代碼當我在瀏覽器中運行Meteor.call('test'),我在控制檯看到以下的輸出:

Inside get_git_commit_hash:d53ffc7f5db26c6e2b40bfcce7a1e2e0d6610ece 
Call 1: 
Call 2:Dummy result 

任何人可以幫助我瞭解爲什麼我不在第一個電話中獲得結果?

回答

5

我不知道這是否會工作,但不妨一試:

runCmd = Meteor.wrapAsync(exec) 

var result = runCmd("git rev-parse HEAD"); 

console.log(result); 

然後你可以後處理結果。另外請注意,您已完成stdout-1,對於您正在運行的命令應該始終返回NaN,而不是數字。

更新

Meteor._wrapAsync現在Meteor.wrapAsync

+0

感謝Akshat,這是不是與child_process問題。這是一個stdout - 1個錯字。它應該是stdout.length - 1.你的方法當然也適用。 –