Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  117] [ 6]  / answers: 1 / hits: 45870  / 12 Years ago, thu, may 17, 2012, 12:00:00

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



this.logMeIn = function(username,stream) {
if (username === null || username.length() < 1) {
stream.write(Invalid username, please try again:nr);
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:



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?


More From » string

 Answers
173

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



String(null) === null


So you need to change your condition:



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

[#85524] Wednesday, May 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;