1

I have a very basic javascript function where I am splitting the string into two parts. The problem is the second part is 'null' and not null. So if function is not working properly

Please find below code and console.logs.

Why such strange behavior. Thank you in advance

  extractCredentials(request: Request): any {

    const authHeaderValue = request.headers.authorization;

    const parts = authHeaderValue.split(' ');
    const encryptedCredentails = parts[1];

    console.log(typeof encryptedCredentails) // prints string
    console.log(encryptedCredentails) // null
    console.log(encryptedCredentails.length) // prints 4

    if (encryptedCredentails == 'null') {
      console.log('null') // prints null
    }
    else {
      console.log('not null') // not executed
    }
    if (encryptedCredentails) {
      console.log('true') // true
    }
    else {
      console.log('false') // not executed
    }
    return encryptedCredentails
  }
pratik jaiswal
  • 1,855
  • 9
  • 27
  • 59
  • are you looking to compare & validate to 'null' value ?? then it should be encryptedCredentails == null – Midhun Murali Oct 23 '20 at 11:37
  • the authHeaderValue is a string. you are implying a string can be null. – GottZ Oct 23 '20 at 11:38
  • No, actually I want to check where the value is null or not – pratik jaiswal Oct 23 '20 at 11:38
  • 2
    `'null'` != `null` – Yousaf Oct 23 '20 at 11:38
  • 1
    I guess your authorization header contains something like `Bearer null` – Guerric P Oct 23 '20 at 11:39
  • 1
    A string can't be null. It's a string. – alexP Oct 23 '20 at 11:39
  • you are doing a `.split(' ')` there.. if authHeaderValue was null, it would throw an exception – GottZ Oct 23 '20 at 11:39
  • 1
    if the split gives you `'null'` (the string variant, and not the object one), then it is because there is a literal *null* in your authorization header. Please use dev console to check it and check the originating code where the auth headers are being set. – KarelG Oct 23 '20 at 11:40
  • This is happening when auth headers are not getting set. When set no problem. – pratik jaiswal Oct 23 '20 at 11:42
  • 1
    [`authHeaderValue.split(' ')`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) always produces an [array of strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Return_value). If `parts[1]` is the string `'null'` (it cannot be the `null` value) then this means that the second word of `authHeaderSplit` is "null". – axiac Oct 23 '20 at 11:46
  • What is the value of `authHeaderValue` when the problem happens? – axiac Oct 23 '20 at 11:50

3 Answers3

3

Just parse it like this JSON.parse(encryptedCredentails ) it return you null without ''

var encryptedCredentails  = 'null'
console.log(JSON.parse(encryptedCredentails )) //null
 console.log(encryptedCredentails ) //"null"
abhay
  • 642
  • 5
  • 13
  • Why `JSON.parse()`? There is no JSON in this question. – axiac Oct 23 '20 at 11:47
  • "null" is a valid JSON. that's why I use JSON.parese to pass the "null". Recently I use one API that returns "null" In response so when I parse it returns null without ".same situations here. – abhay Oct 23 '20 at 12:22
0

This condition is checking for a string value of 'null', not the primitive type null.

    if (encryptedCredentails == 'null') {
      console.log('null') // prints null
    }

If you are trying to do a null check on encryptedCredentails then your condition should be one of the following possible options:

If you are only trying to check for null values, and not other null-ish values, then do a 'strict' equality check:

if (encryptedCredentails === null)

If you want to treat undefined 0 etc etc as `null, then use:

if (encryptedCredentails == null)

or

if (!encryptedCredentails)

see similar question on checking for null values here

amyloula
  • 1,556
  • 2
  • 17
  • 26
0

You are splitting on the basis of whitespace " ", so it cannot be "not null" because it will split it also

Sidra Asghar
  • 49
  • 3
  • 11