Carol.Kar
Carol.Kar

Reputation: 5355

Showing fields of queried [object Object] in console.log

I am using "express": "3.2.6",, nodeJS v0.10.25 and "express-myconnection": "1.0.4",.

My database connection seems to work. However, when going to my view to query:

console.log("Data: " + rows);

I get the following output:

Data: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

My query method looks like the following:

exports.list = function(req, res){
    req.getConnection(function(err,connection){

        connection.query('SELECT * FROM customer',function(err,rows)     {

            if(err)
                console.log("Error Selecting : %s ",err );

            console.log("Data: " + rows);
            res.render('index.ejs',{page_title:"Customer - Node.js",data:rows});

        });

    });
};

My customer table looks like that:

CREATE TABLE `customer` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `street` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `timestamp` bigint(20) DEFAULT NULL,
 `typeOfTransaction` enum('buy','sell') COLLATE utf8_unicode_ci DEFAULT NULL,
 `price` double DEFAULT NULL,
 `quantity` double DEFAULT NULL,
 `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Any recommendations how I can display the data as strings?

Upvotes: 2

Views: 99

Answers (1)

James Donnelly
James Donnelly

Reputation: 128786

That's because you're using string concatenation (string + object).

Change:

console.log("Data: " + rows);

To:

console.log("Data:", rows);

--

Demo

var obj = { foo: 'bar' };

console.log("Data: " + obj);
console.log("Data:", obj);

Upvotes: 3

Related Questions