node.js - Receive all elements of database with npm orm -
i use npm orm , can create new database entries. want receive entries not work expected. used .all
function receive entries didn't work.
app
var express = require('express'); var path = require('path') var app = express(); var bodyparser = require('body-parser'); var orm = require('orm'); var db = orm.connect('mysql://root:fdb2bj6p@localhost/idangmanagement', function(err, db) { if (err) throw err; }); module.exports = db; var person = db.define('person', { id: { type: 'serial', key: true }, name: { type: 'text' }, email: { type: 'text' } }); // views app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // join public dir app.use(express.static(path.join(__dirname, 'public'))); // parse stuff app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); // routes app.get('/', function(req, res) { person.all(function(err, persons) { if(err) return err; console.log(persons, persons.name) }); res.render('index'); }); app.post('/add', function(req, res) { var new_person = {}; new_person.name = req.body.name; new_person.email = req.body.email; person.create(new_person, function(err, results) { if (err) throw err; }); }); var server = app.listen(3000, function() { var host = server.address().address; var port = server.address().port; console.log('example app listening @ http://%s:%s', host, port); });
when print persons.name
receive undefined. output of persons:
[ { id: [getter/setter], name: [getter/setter], email: [getter/setter] } ]
plus receive no errors. guys know why not work?
try
person.find({}, function(err, persons) { if(err) return err; console.log(persons, persons.length); });
or even:
person.find(function(err, persons) { if(err) return err; console.log(persons, persons.length); });
does work you?
edit*
loop through persons:
person.all(function(err, persons) { persons.foreach(function(person){ console.log(person); }); });
Comments
Post a Comment