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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8457389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:05:30  来源:igfitidea点击:

node.js displays "undefined" on the console

javascriptnode.jsconsoleundefined

提问by Sandeep G B

Recently I installed node.js on my Windows 7 machine.

最近我在我的 Windows 7 机器上安装了 node.js。

On execution of JavaScript, I get an undefinedmessage along with successful execution of the expression.

在执行 JavaScript 时,我收到一条未定义的消息以及表达式的成功执行。

What's wrong here? I have not noticed any other side effects.

这里有什么问题?我没有注意到任何其他副作用。

enter image description here

在此处输入图片说明

采纳答案by Esailija

Just write "hello world";and hit enter... it will return "hello world"instead of undefined, thus no undefinedis displayed. console.logreturns undefinedand also logs arguments to console so you get multiple messages.

只需写入"hello world";并按回车键...它将返回"hello world"而不是undefined,因此不undefined显示。console.log返回undefined并将参数记录到控制台,以便您获得多条消息。

回答by alessioalex

The JavaScript functions always return something. If you don't specify something to return in the function, 'undefined' is returned by default (you can check this out in Firebug too).

JavaScript 函数总是返回一些东西。如果您没有在函数中指定要返回的内容,则默认返回 'undefined'(您也可以在 Firebug 中查看)。

Don't worry though, this doesn't affect anything, you can ignore it.

不过不用担心,这不会影响任何事情,您可以忽略它。

回答by AvinashB

As pointed out by others, javascript function will always return undefined if you do not specify any return value. 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 has this property ignoreUndefinedwhich is set to false by default. You can set it to true. Try this:

正如其他人所指出的,如果您不指定任何返回值,javascript 函数将始终返回 undefined。你可以忽略它。它不会造成任何伤害。但是如果它让你太烦了,那么你可以在 repl 中关闭它。Repl 有这个属性ignoreUndefined,默认设置为 false。您可以将其设置为 true。尝试这个:

module.exports.repl.ignoreUndefined = true;

回答by Melo Waste

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 undefinednormally and you can choose a better return value. Since Non-strict (abstract) comparison considers undefinedequal to falsethe ||operator allows you that choice.

如前所述,函数正常console.log()返回undefined,您可以选择更好的返回值。由于非严格(摘要)相比,认为undefined等于false||运营商允许你这样的选择。

Considering that it is just a debugging tool, this is unnecessary in general use, but helps to understand that console displaystext sent to stdout and also evals the command and displaysthe returned value, or undefined if no value was received.

考虑到它只是一个调试工具,这在一般使用中是不必要的,但有助于理解控制台显示发送到 stdout 的文本并评估命令并显示返回值,或者如果没有收到值则为 undefined。

回答by xameeramir

nodealso prints undefinedbecause IN GENERAL, it displays the return value of each command and console.logdoesn't return anything.

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

回答by RienNeVaPlu?s

If you need to turn on ignoreUndefinedfrom inside a module, you can use the following line to simulate AvinashB's answer without typing into the console by yourself:

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

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

回答by Mahmoud Sayed

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'));

回答by Selvaganesan Saminathan

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

回答by Ken Lin

To avoid seeing undefinedoutput from commands when using Node's replmodule, set ignoreUndefinedto truein repl.start.

为了避免undefined在使用 Noderepl模块时看到命令的输出,设置ignoreUndefinedtruein repl.start