2

I have an issue with extracting value from object. I want to check if my SQL table have the asked row:

var checkRow = function(connection,chekedColumn,chekParam, checkVal){
    connection.query('SELECT EXISTS(SELECT '+ chekedColumn +' FROM asterisk.users where ' +chekParam+'='+checkVal+')', function (err, rows) {
        if(err) console.log('SQL suerry error')
        else {
            console.log(rows[0]); // output: { 'EXISTS(SELECT id FROM asterisk.users where name=1600)': 1 }
            return (rows[0]);
        }; 
    }); 
};

but returned value from query is an object, and return(rows[0]) give just [object object] output. How can I extract value of this object?

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
Alex Cebotari
  • 291
  • 2
  • 4
  • 9
  • As I know that in javascript access object with dot (.) keyword don't use this rows[0] try this if I'm not wrong rows.0 – user2727841 Oct 03 '13 at 06:13
  • rows.0 not work: SyntaxError: Unexpected number – Alex Cebotari Oct 03 '13 at 06:18
  • rows[0] is fine, rows.0 is not, 0 is not valid property name. See valid property names for access via . [here](http://stackoverflow.com/questions/1661197/valid-characters-for-javascript-variable-names). – user568109 Oct 03 '13 at 06:23
  • change the index from 0 to 1 may be you've got the value – user2727841 Oct 03 '13 at 06:23
  • 1
    You cannot do return from asynchronous function to get the value. You should call your function to process the returned value from inside the query. – user568109 Oct 03 '13 at 06:27
  • @user2727841 decimal digits alone do not form valid property names. You can't use it via `.`. – user568109 Oct 03 '13 at 06:33

3 Answers3

2

You can use

Object.keys(obj) 

to get the values of the object. See api reference for more info.

EDIT:

Just to elaborate abit more on this... how you'd go about getting it out is this.

// get the keys of the source
var keys = Object.keys( source );
// loop through the keys
for ( var i = 0; i < keys.length; i++ ) {
  var key = keys[i];
  var value = source[key];

  //do something with value
}
Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43
  • var keys=Object.keys(rows[0]); var key=keys[0]; console.log(rows[key]);// output : undefined – Alex Cebotari Oct 03 '13 at 06:42
  • console.log(key);// output: EXISTS(SELECT id FROM asterisk.users where name=1600) – Alex Cebotari Oct 03 '13 at 06:44
  • console.log(rows[0].key);// output: undefined – Alex Cebotari Oct 03 '13 at 06:46
  • console.log(key); <--- this is the value inside the unknown object property (I assume this is what is getting assigned as a parameter to a query callback function which is the answer to your question). If the value is not what you expect I suggest you debug your code and see what happens, consider rephrasing your question since this is a different matter altogether. There is a good tool for debugging node apps which is called [NodeWebKit](https://github.com/rogerwang/node-webkit) – Dmitry Matveev Oct 03 '13 at 08:06
0

The output is an object which you don't know. So try either:

  1. console.log("Result: %j", rows[0]);
  2. console.log(JSON.stringify(rows[0]));
  3. console.log(require('util').inspect(rows[0], false, null));

to view the structure. After you know the keys, use it to access the data.

user568109
  • 47,225
  • 17
  • 99
  • 123
0

Thanks all for helps and for your ideas. Dmitry Matveev help me to understand how to identify object properties(Objekt.keys()). And thanks to user568109 for reminding of asynchronous function, so I use callback.
Final code:

var checkRow = function(connection,chekedColumn,chekParam, checkVal,callback){
    connection.query('SELECT EXISTS(SELECT '+ chekedColumn +' FROM asterisk.users where ' +chekParam+'='+checkVal+')', function (err, rows) {
                if(err) console.log('SQL suerry error')
                else {
                   var keys=Object.keys(rows[0]);
                   keys.forEach(function(key) {
                       callback(rows[0][key]);
                     });
                    }; 
        }); 

};

And for using this functions we need to call it like:

checkRow(connection,'id','name',name,function(value){
 console.log(value)
})
Alex Cebotari
  • 291
  • 2
  • 4
  • 9
  • everyone on stackoverflow would love you if you start marking answers as accepted that helped you to figure out your problem ;) – Dmitry Matveev Jan 17 '16 at 21:44