1

I have the following query for a PostgreSQL databse using knex:

knex('mytable').select('name').then(function(rows) {
        console.log(rows[1].name);
        var a = "Test";
        var b = rows[1].name;
        console.log(a + "  " + b)
    })

The query is working however the "rows[1].name" value is a... object thingy whatever which looks like {"value"} instead of simply a string containing the value 'value'.

My question here is: Am I doing something "wrong" ? Are we generally speaking supposed to work with this type of values when using SQL databases rather than plain old string values ? If so how exactly should i treat these objects (say if I wished to display the value inside of it on an html page)?

Furthermore, if I am to convert this object to a string, is there a knex function that allows me to do so (obviously I can do it using plain of js and substr but I'd think it would be rather inefficient, possibly not "The right way" to do such a thing) ?

  • If you database column has json type, it will be converted to object automatically by knex. Can you check what type `name` column has? – Vsevolod Goloviznin Jun 13 '16 at 09:21
  • The following question is directly related: [How to make pg-promise return rows as arrays?](http://stackoverflow.com/questions/36020663/how-to-make-pg-promise-return-rows-as-arrays), because the underlying driver is the same (`node-postgres`), and `knex` should also support the `rowMode` parameter somehow, and if it doesn't - that's a knex issue. – vitaly-t Jun 13 '16 at 16:04
  • @ Vsevolod Goloviznin Its of type string, wouldn't type Json be a waste of memory/a useless abstraction considering that my name column only holds short strings (20 char or less) – PrivacyIs Gone Jun 14 '16 at 16:46

0 Answers0