简体   繁体   English

node.js 在控制台上显示“未定义”

[英]node.js displays "undefined" on the console

Recently I installed node.js on my Windows 7 machine.最近我在我的 Windows 7 机器上安装了 node.js。

On execution of JavaScript, I get an undefined message along with successful execution of the expression.在执行 JavaScript 时,我收到一条未定义的消息以及表达式的成功执行。

What's wrong here?这里有什么问题? I have not noticed any other side effects.我没有注意到任何其他副作用。

在此处输入图片说明

The JavaScript functions always return something. JavaScript 函数总是返回一些东西。 If you don't specify something to return in the function, 'undefined' is returned by default (you can check this out in Firebug too).如果您没有在函数中指定要返回的内容,则默认返回 'undefined'(您也可以在 Firebug 中查看)。

Don't worry though, this doesn't affect anything, you can ignore it.不过不用担心,这不会影响任何事情,您可以忽略它。

Just write "hello world";只写"hello world"; and hit enter... it will return "hello world" instead of undefined , thus no undefined is displayed.并按回车键...它将返回"hello world"而不是undefined ,因此不会显示undefined console.log returns undefined and also logs arguments to console so you get multiple messages. console.log返回undefined并将参数记录到控制台,以便您获得多条消息。

As pointed out by others, javascript function will always return undefined if you do not specify any return value.正如其他人所指出的,如果您不指定任何返回值,javascript 函数将始终返回 undefined。 You can just ignore it.你可以忽略它。 It's not going to cause any harm.它不会造成任何伤害。 But if it's annoying you too much then you can turn it off in repl.但是如果它让你太烦了,那么你可以在 repl 中关闭它。 Repl has this property ignoreUndefined which is set to false by default. Repl 有这个属性ignoreUndefined ,默认设置为 false。 You can set it to true.您可以将其设置为 true。 Try this:尝试这个:

module.exports.repl.ignoreUndefined = true;

Well, the question was made some years ago, but there is still another way to explain what is happening here.好吧,这个问题是几年前提出的,但还有另一种方式来解释这里发生的事情。

Try next command:尝试下一个命令:

console.log("Hello World") || "Bye World";

As mentioned function console.log() returns undefined normally and you can choose a better return value.如前所述,函数console.log()正常返回undefined ,您可以选择更好的返回值。 Since Non-strict (abstract) comparison considers undefined equal to false the ||由于非严格(抽象)比较认为undefined等于false || operator allows you that choice.运营商允许您进行选择。

Considering that it is just a debugging tool, this is unnecessary in general use, but helps to understand that console displays text sent to stdout and also evals the command and displays the returned value, or undefined if no value was received.考虑到它只是一个调试工具,这在一般使用中是不必要的,但有助于理解控制台显示发送到 stdout 的文本并评估命令并显示返回值,或者如果没有收到值则为 undefined。

如果您需要从模块内部打开ignoreUndefined ,您可以使用以下行来模拟AvinashB的答案,而无需自己在控制台中输入:

process.stdin.emit('data', "module.exports.repl.ignoreUndefined = true;\n");

node也会打印undefined因为IN GENERAL ,它显示每个命令的返回值,并且console.log不返回任何内容。

this is happening because console.log has (null/undefined) return type to solve this issue.. have a function returning a string.. then log that function returned value... that will overcome the undefined issue..这是因为 console.log 有(空/未定义)返回类型来解决这个问题..有一个函数返回一个字符串..然后记录该函数返回的值......这将克服未定义的问题..

const addNotes= function(msg)
{
    return msg;
}
console.log(addNotes('say hello'));

Just ignore it, no need to worry at all.直接无视就好,完全不用担心。 I too had the same issue when I called the function without any return type.当我在没有任何返回类型的情况下调用函数时,我也遇到了同样的问题。 So node used to print undefined return type for the called function.所以节点用于打印被调用函数的未定义返回类型。

function emitEvent(eventname){
    eventEmitter.emit(SIV_EVENT);
}
console.log(emitEvent());

Check undefined here, https://nodejs.org/api/util.html .在此处检查未定义, https://nodejs.org/api/util.html

为了避免看到undefined使用节点的时候,从命令的输出repl模块,设置ignoreUndefinedtruerepl.start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM