Sunday 15 August 2010

mysql - ejs templating engine with express 4, foreach fuction for object don't work -



mysql - ejs templating engine with express 4, foreach fuction for object don't work -

i utilize ejs 1.0 , express 4 , have problem iteration. seek utilize foreach object returned database (mysql). code express:

var express = require('express'); var app = express(); var bodyparser = require('body-parser'); var mysql = require('mysql'); var path = require('path'); var db = require('./config/db.js'); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.set('view engine', 'ejs'); app.set('views', __dirname + '/views'); app.use(express.static(path.join(__dirname, 'src'))); var router = express.router(); router.route('/todo') .post(function(req, res, next){ db.query('insert task set todo_title = ?, todo_description =?',[req.body.title, req.body.description], function(err, info){ if(err) { console.log('db error', err); } console.log('zadanie dodano z id: '); res.redirect('/api/todo'); }); }).get(function(req, res, next){ db.query('select todo_id, todo_description, info task', function(err, result){ if(err) throw err; var info = json.stringify(result); res.render('pages/index', {items: data}); }); }); router.route('todo/:todo_id') .get(function(req, res){ }).post(function(req, res){ }).delete(function(req, res){ }); app.use('/api', router); app.listen(3000);

and template:

<!doctype html> <html> <head> <% include ../partials/header %> </head> <body class="container"> <section id="main"> <h1>welcome!</h1> <ul> <% items.foreach(function(item) { %> <li><% item.todo_id %></li> <% }); %> </ul> <form action="/api/todo" method="post"> <label for="title">nazwa zadania</lablel><br> <input name="title" type="text"><br> <label for="description">opis zadania</label><br> <textarea name="description"></textarea><br> <button>wyƛlij</button> </form> </section> <footer> <% include ../partials/footer %> </footer> </body> </html>

i seek for() don't work to. object returned route:

[{"todo_id":1,"todo_description":"cos2","data":"2014-10-22t13:02:53.000z"},{"todo_id":2,"todo_description":"cos2","data":"2014-10-22t13:03:13.000z"},{"todo_id":3,"todo_description":"cos2","data":"2014-10-22t13:05:00.000z"},{"todo_id":4,"todo_description":"cos2","data":"2014-10-22t13:06:03.000z"},{"todo_id":5,"todo_description":"cos2","data":"2014-10-22t13:13:31.000z"},{"todo_id":6,"todo_description":"asd","data":"2014-10-22t13:15:39.000z"}]

the issue utilize of stringify():

db.query('select todo_id, todo_description, info task', function(err, result){ // ... var info = json.stringify(result); res.render('pages/index', {items: data}); });

string values, stringify() creates, don't have .foreach() method. and, res.render() , ejs don't require values json-encoded.

you can assign result, array of objects, items without involving json:

res.render('pages/index', { items: result });

or, need parse() within view:

<% json.parse(items).foreach(function(item) { %>

mysql node.js express ejs embedded-javascript

No comments:

Post a Comment