2011-07-22 40 views
0

我有以下Node.js的文件:node.js的 「不確定」 神祕出現

//test.js: 
var hash=require('./hash'); 
var sys=require('sys'); 

sys.puts(hash.hash("asdf","asdf")); 

//hash.js: 
var exec=require('child_process').exec; 
var sys=require('sys'); 

exports.hash=function(data,hash_type){ 
     exec('pwd',function callback(error,stdout,stderr){ 
       sys.puts(stdout); 
     }); 
} 

當我做node test.js,我得到下面的輸出:

[email protected]:~/Desktop/simple-hash$ node test.js 
undefined 
/home/hynese/Desktop/nodejs-simple-hash 

爲什麼我得到「undefined」?我真的卡在這裏...

任何幫助,非常感謝。

提前許多感謝,

回答

3

變化:

sys.puts(hash.hash("asdf","asdf")); 

只是:

hash.hash("asdf","asdf"); 

你輸出的hash.hash的返回值,但是因爲你沒有提供返回值,語言返回undefined,然後輸出到屏幕。您已經在回調中輸出了系統命令的結果,因此您不需要另一個sys.puts

作爲便箋,您可能不需要命名該回調函數callback

+0

Doh! *劉海桌子* *非常感謝... – Eamorr

+0

@Eamorr,一種快樂:) – davin