javascript - How to send a query result with sequelize between model and controller -
my problem following:
i have nodejs+expressjs , generated mvc project generator-express have mysql+sequelize+gulp app.
i connected db , made query in model, can't pass result controller , print in screen.
instead of error variable undefined.
the code model (regiones.js) is:
module.exports = function (sequelize, datatypes) { var regiones = sequelize.define('regiones', { idregion: datatypes.integer, nombre: datatypes.string }, { classmethods: { encontrar : function(){ sequelize .query('select * regiones', { raw: true }) .spread(function(resul, m){console.log(resul); return resul;}); } } }); return regiones };
the code controller (home.js) is:
var express = require('express'), router = express.router(), db = require('../models'); module.exports = function (app) { app.use('/', router); }; router.get('/', function (req, res, next) { resu = db.regiones.encontrar(); console.log("resu : "+resu); var arreglo = []; for(i=0;i<resu.length;i++){ arreglo.push(resu[i].nombre); } db.article.findall().then(function (articles) { res.render('index', { title: arreglo[1], articles: articles }); }); });
"regiones" (sorry because names in spanish) has "idregiones" , "nombre".
basically want know how result of query in controller.
the index prints only:
cannot read property 'length' of undefined typeerror: cannot read property 'length' of undefined @ c:\users\erick\farma1\app\controllers\home.js:15:17 @ layer.handle [as handle_request]
and console prints tuples in model, in controller prints:
resu : undefined
i've search lot, seems theoretical problem of me.
thanks :)
your encontrar
function not return anything, that's why getting undefined.
the query async, need add handler returned promise:
db.regiones.encontrar().then(function (resu) {...
Comments
Post a Comment