Sunday, 23 April 2017

Cant get array data on mysql with nodejs

var express = require('express')
var app = express()
var mysql = require('mysql');
app.listen(3000)
app.engine('html', require('ejs').renderFile)
app.get('/product',showConnect)
var mysql_conn = mysql.createConnection({
    host: 'x.x.x.x',
    user: 'user',
    password: 'password',
    database: 'db'
});

When I run with this, it get correct value

mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE `query_text` LIKE "%drawing eye%" ORDER BY  `popularity` DESC LIMIT 0 , 10', function(err, rows) {
    if (err) {
        throw err;
    }
    for (id in rows) {
        console.log(JSON.stringify(rows[id]));
    }
}); 

but when I render to page with this code

function showConnect(req, res){
    mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE `query_text` LIKE "%drawing eye%" ORDER BY  `popularity` DESC LIMIT 0 , 10', function(error, rows){
    res.render('product.html', {users: rows})
    })
}

on product.html

<%
for (var i of coffee){
    %>
        <%= i %>
    <%
}
%>

it show this data

[object Object] [object Object] [object Object] [object Object] [object Object] [object Object]

I try to run with JSON.stringify() but it show like this

[ { " q u e r y _ t e x t " : " E t u d e H o u s e D r a w i n g E y e B r o w ( N e w ) " } , { " q u e r y _ t e x t " : " E t u d e H o u s e D r a w i n g E y e B r o w ( N e w ) 1 D a r k B r o w n " } , { " q u e r y _ t e x t " : " E t u d e H o u s e D r a w i n g E y e B r o w N e w 4 " } , { " q u e r y _ t e x t " : " E t u d e H o u s e D r a w i n g E y e B r o w " } , { " q u e r y _ t e x t " : " E t u d e d r a w i n g e y e b r o w " } , { " q u e r y _ t e x t " : " e t u d e d r a w i n g e y e b r o w E t u d e H o u s e " } , { " q u e r y _ t e x t " : " d r a w i n g e y e " } , { " q u e r y _ t e x t " : " d r a w i n g e y e b r o w " } , { " q u e r y _ t e x t " : " E t u d e d r a w i n g e y e " } , { " q u e r y _ t e x t " : " e t u d e h o u s e d r a w i n g e y e b r o w d u o " } ]

My question is 'How can I get data without space? Also need only value without rows name?



via Moomoo Soso

No comments:

Post a Comment