Javascript 当字符串可能为空时,在 Node JS 中确定字符串长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10635727/
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 02:14:36  来源:igfitidea点击:

Determining string length in Node JS when string may be null

javascriptstringnode.jsif-statementnull

提问by Amateur Programmer by Night

I'm trying to learn Node and have the function:

我正在尝试学习 Node 并具有以下功能:

this.logMeIn = function(username,stream) {
  if (username === null || username.length() < 1) {
    stream.write("Invalid username, please try again:\n\r");
    return false;
  } else {
  ....etc

and I'm passing it

我正在通过它

if (!client.loggedIn) {
  if (client.logMeIn(String(data.match(/\S+/)),stream)) {

I've tried both == and ===, but I'm still getting errors as the username is not detecting that it is null, and username.length() fails on:

我已经尝试了 == 和 ===,但我仍然收到错误,因为用户名没有检测到它为空,并且 username.length() 失败:

if (username === null || username.length() < 1) {
                                  ^
TypeError: Property 'length' of object null is not a function

I'm sure that Node won't evaluate the second part of the || in the if statement when the first part is true - but I fail to understand why the first part of the if statement is evaluating to false when username is a null object. Can someone help me understand what I've done wrong?

我确信 Node 不会评估 || 的第二部分 在 if 语句中,当第一部分为真时 - 但我不明白为什么当用户名是空对象时,if 语句的第一部分评估为假。有人可以帮助我了解我做错了什么吗?

采纳答案by Engineer

You're passing String(data.match(/\S+/))as usernameargument, so when data.match(/\S+/)is null, you get "null"not nullfor username, as:

String(data.match(/\S+/))作为username参数传递,所以当data.match(/\S+/)is 时null,你得到"null"not nullfor username,因为:

String(null) === "null"

So you need to change your condition:

所以你需要改变你的条件:

if( username === null || username === "null" || username.length < 1 )

回答by Amberlamps

lengthis an attribute, not a function. Try username.length

length是一个属性,而不是一个函数。尝试username.length

回答by Ritu

Try

尝试

if( username === null || username.toString().length < 1 )

if( username === null || username.toString().length < 1 )

I used if( username === null || username.length < 1 ) and it failed at length check.

我使用了 if( username === null || username.length < 1 ) 并且它在长度检查中失败了。

回答by jmar777

If you require a non-empty string, you can do a simple "truthy" check that will work for null, undefined, '', etc:

如果你需要一个非空字符串,你可以做一个简单的“truthy”检查,会为工作nullundefined'',等:

if (username) { ... }

With that approach, you don't even need the .lengthcheck. Also, lengthis a property, not a method.

使用这种方法,您甚至不需要.length支票。此外,length是一个属性,而不是一个方法。



Edit: You have some funkiness going on. I think you need to start with how you're passing in your username - I don't think that your String(data.match(/\S+/))logic is behaving the way that you're expecting it to (credit to @Engineer for spotting this).

编辑:你有一些时髦。我认为你需要从你如何传递你的用户名开始——我认为你的String(data.match(/\S+/))逻辑并不像你期望的那样(归功于@Engineer 发现了这一点)。

Your match expression is going to return one or two types of values: nullor an Array. In the case that it's null, as @Engineer pointed out, you end up passing in "null"as a string, which should resultantly pass your username check later on. You should consider revising this to:

您的匹配表达式将返回一种或两种类型的值:nullArray. 在它为空的情况下,正如@Engineer 所指出的那样,您最终会"null"以字符串形式传入,因此稍后应该会通过您的用户名检查。您应该考虑将其修改为:

if (!client.loggedIn) {
    var matches = data.match(/\S+/);
    if (client.logMeIn(matches ? matches[0] : '',stream)) {

Regarding .lengthbeing equal to 1in all cases - that doesn't honestly make a lot of sense. I would recommend adding a lot of console.log()statements to try and figure out what's going on.

关于在所有情况下.length都等于1- 老实说,这并没有多大意义。我建议添加很多console.log()语句来尝试弄清楚发生了什么。