开发者

node.js "undefined" mysteriously appears

开发者 https://www.devze.com 2023-03-22 09:50 出处:网络
I have the following node.js files: //test.js: var hash=require(\'./hash\'); var sys=require(\'sys\'); sys.puts(hash.hash(\"asdf\",\"asdf\"));

I have the following node.js files:

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

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

and

//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);
        });
}

When I do node test.js, I get the following output:

eamorr@Compaq6000:~/Desktop/simple-hash$ node test.js 
undefined
/home/hynese/Desktop/nodejs-simple-hash

Why am I getting "undefined"??? I'm really stuck here...

Any help is gr开发者_开发知识库eatly appreciated.

Many thanks in advance,


Change:

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

to just:

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

You're outputting the return value of hash.hash, although since you didn't provide a return value, the language returns undefined, which is then outputted to the screen. You already output the result of the system command in the callback, so you don't need another sys.puts.

As a side note, you probably don't need to name that callback function callback.

0

精彩评论

暂无评论...
验证码 换一张
取 消