0

Here are my codes:

app.get('/index/:name',function(req, res){
    data = connection.query('SELECT * FROM deneme', function(err, rows, fields){
        if(err) throw err;
        veri = rows;
        return veri;
    });

    console.log(data);

    res.render('index', {
        title: req.params.name,
        para: data
    });
});

When i call localhost:8080/index/example it returns [object Object] I want to print data of array, how can i do this?

I can do it when I'm using php with print_r() function..

Meantime, array's data:

Id Name
1  yab
2  sad
0

4 Answers 4

2

I think OP just want to print the data for debugging purposes, for that you can use the following cross-browser way that uses JSON to print a nice representation of the data:

console.log( JSON.stringify(data, null, 4) );
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should work:

for (var colName in data) {
    console.log("column " + colName);
    var rows = data[colName];
    for (var i = 0; i < rows.length; i++) {
        console.log(rows[i]);
    }
}

You will have to tweak it for your needs, that's the general way to iterate such objects.

Comments

0

You need to iterate the result collection and print the attributes

      $.each(resultArray, function() {
             this.id;//use this to write id
             this.name;//use this to write name
       });

1 Comment

@user1870012 This is JQuery syntax.
0

To output to the console you could do something like:

for(var index in data) {
    var item = data[index];
    var output = [];
    for(var field in item) {
        output.push(item[field]);
    }
    console.log(output.join("\t"));
}

For outputting in html, you'd do something like similar, but creating actual HTML nodes. Maybe, something like:

var output = ["<table>"];
for(var index in data) {
    output.push("<tr>");
    var item = data[index];
    for(var field in item) {
        output.push("<td>");
        output.push(item[field]);
        output.push("</td>");
    }
    output.push("</tr>");
    console.log(output.join("\t"));
}
output.push("</table>");
element.innerHTML = output.join("");

I've not tried that in a browser, so there might be a couple of mistakes, but that should do it.

Let me know if you'd like any more help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.