0

I am making a script in Javascript script that gets a SQL response, then processes it. Basically, I want to check if the username value exists in result[1]. When it checks, it errors out and says that it does not exist. If it does not exist, I want it to return false, not stop the program.

Here is the code:

if (result[1].username != undefined) {
    return true;
} else {
    return false;
}

I have tried using typeof(result1) == undefined, but it gives me the same error.

Robert Columbia
  • 6,012
  • 14
  • 28
  • 36
Robert Westbury
  • 64
  • 3
  • 10
  • You need to use strict comparison `!==` instead of weak comparison `!=`. For `typeof()` check you need to compare with string `'undefined'` instead of `undefined` instead as `typeof()` returns string. Also you have `result1` instead of `result[1]`, maybe it is a reason. – Flying Nov 01 '17 at 18:11
  • it is dependent of you sql api. basically you could check if the element of th array exists, if the property exits and/or if the vlaue is truthy or not empty string. – Nina Scholz Nov 01 '17 at 18:12
  • 1
    Can you specify what error you are getting – Krishjs Nov 01 '17 at 18:18
  • This can't be answered without guessing until the error message being received is given. At this point for all we know `result[1]` might not even exist. Once we have more info this can be dupe closed. – Kevin B Nov 01 '17 at 19:03
  • Possible duplicates: https://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object | https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property | https://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript – Kevin B Nov 01 '17 at 19:07

3 Answers3

0

You could use the in operator. For example:

let trueObj = { username: 'Foo' };
let falseObj = { };

if ('username' in trueObj) {
  console.log('username found in trueObj');
} else {
  console.log('username not found in trueObj')
}

if ('username' in falseObj) {
  console.log('username found in falseObj');
} else {
  console.log('username not found in falseObj')
}
Olian04
  • 4,754
  • 2
  • 23
  • 46
0

First, you have to make sure the result exists, otherwise you'd be indexing into undefined which would crash your application.

Second, you can make that check less verbose with:

return (result[1] && result[1].username)

which will return a falsey value if it doesn't exist, and whatever the username is, if it does.

In case you need an explicit true to be what the function returns, you can coerce it:

return (result[1] && (result[1].username && true))

I would make sure to refactor for readability, but that's the gist.

Sua Morales
  • 816
  • 8
  • 20
-2

First of all please check whether the result itself exists or not and make the corresponding & operator and i think this will definitely help

if (result && result[1] && result[1].username) {
    return true;
} else {
    return false;
}

But if you don't want to make your code complex then you can try lodash library. https://lodash.com/

Atul Agrawal
  • 1,208
  • 3
  • 16
  • 35