First of all, all my code is done in node.js but this can all be applied to javascript too.
This is my code I use to check if the keys exist, the problem is that it always returns false. So I added in the console.log to shows what the values are:
if(!choice.name || !choice.realm || !choice.region || !choice.roll){
console.log(choice);
console.log(choice.name);
console.log(choice.realm);
console.log(choice.region);
console.log(choice.roll);
return false;
}
This is the output of that:
{"name":"Imacactus","realm":"Velen","region":"US","roll":"DPS"}
undefined
undefined
undefined
undefined
I'm guessing it has something to do with the quotes? but I've never heard of quotes messing it up. Is this a node.js problem? I've also tried .hasOwnProperty('realm') and it still failed.
This is most of the code with all the functions: http://pastebin.com/DUN9VdHr
You need to parse your json into a javascript object before you can reference its properties.
You can use JSON.parse
var choiceobj = JSON.parse(choice);
if(!choiceobj.name || !choiceobj.realm || !choiceobj.region || !choiceobj.roll){
console.log(choiceobj);
console.log(choiceobj.name);
console.log(choicepbj.realm);
console.log(choiceobj.region);
console.log(choiceobj.roll);
return false;
}
The problem is that the quotes is part of the key so to access it you have to do something like:
console.log(choice['"name"']);