I'm using a mysql select query in my route and I'm trying to render the data in the view.
I have some trouble retrieving the data and display it with angular ng-repeat. I think it's something to do with the data format (json / array). If someone could light me a bit on this, would appreciate !
PS : I'm using EJS template
Nodejs :
pool.getConnection(function(err, conn) {
var db_result = [];
query = conn.query('SELECT * FROM table');
query.on('error', function(err){
throw err;
});
query.on('result', function(row){
db_result = row;
//Maybe I need to use db_result.push(row) or db_result = JSON.stringify(row);
});
query.on('end', function(result){
res.render('index', { db_data: db_result });
});
conn.release();
});
AngularJS :
//Code inside MainCtrl
$scope.db_datas = <%=db_data%>;
//HTML code
<div ng-repeat="db_data in db_datas | orderBy:'id':true">
<p>{{db_data.id}}</p>
</div>
Edit :
The value of the nodejs variable db_result looks like this :
[
{ id: 104,
content: 'test',
date: Sat Jul 26 2014 03:57:43 GMT+0200 (CEST)
},
{ id: 105,
content: 'test',
date: Sat Jul 26 2014 03:57:43 GMT+0200 (CEST)
}
]