I searched for hours and hours without solving this problem. I'm a very beginner in Node.Js, and I'm currently using it with Express along with Router and Sequelize for a school project.
Context:
I have a MySQL database with a table "products" and two rows: http://i.imgur.com/U5Lqtpo.png
I want to display each id, libelle, type, description and ean13Code for each SQL row.
Problem:
I'm unable to pass the result of a "findAll()" query into the Pug template. I'm constantly faced with this error which is : Cannot read property 'length' of undefined.
Code samples:
Below are the code samples. I hope someone will be able to know what's going on as I have no clue why it isn't working.
Let me know if I forgot to mention something important.
products.pug
extends layout
block body
center
h1= title
p Welcome to #{title}
ul
each product in list_products //<-- Cannot read property 'length' of undefined
li= item.libelle
app.js
'use strict';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var models = require('./models');
var index = require('./routes/index');
var sign_in = require('./routes/sign_in');
var users = require('./routes/users');
var products = require('./routes/products');
var app = express();
models.sequelize.sync({
//force: true
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
//app.use('/sign_In', sign_in);
app.use('/users', users);
app.use('/products', products);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Route Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
index.js (route)
var express = require('express');
var router = express.Router();
/* GET index page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'my title' });
});
/* GET sign page. */
router.get('/sign_in', function(req, res, next) {
res.render('sign_in', { title: 'my title' });
});
/* GET users page. */
router.get('/users', function(req, res, next) {
res.render('users', { title: 'my title' });
});
/* GET products page. */
router.get('/products', function(req, res, next) {
res.render('products', { title: 'my title' });
});
module.exports = router;
products.js (route)
"use strict";
const express = require("express");
const models = require("../models");
const router = express.Router();
const Product = models.Product;
router.post("/", function(req, res, next){
let libelle = req.body.libelle;
let type = req.body.type;
let description = req.body.description;
let ean13Code = req.body.ean13Code;
Product.create({
libelle: libelle,
type: type,
description: description,
ean13Code: ean13Code
}).then(function(prod){
res.json(prod);
}).catch(next);
});
router.get("/", function(req,res,next){
let limit = req.query.limit || 20;
let offset = req.query.offset || 0;
let options = {
limit: limit,
offset: offset
}
let search = req.query.search;
if(search) {
let where = {
$or: {
libelle: {
$like: "%" + s + "%"
},
type: {
$like: "%" + s + "%"
},
ean13Code: {
$like: "%" + s + "%"
}
}
}
options.where = where;
}
Product.findAll().then(function(products){
for(let i in products){
products[i] = products[i].responsify();
}
// I want to send the results of the findAll() to the products.pug page
res.render("/", {list_products: products});
}).catch(next);
});
router.get("/:prod_id", function(req,res, next){
Product.find({
where: { id: req.params.prod_id },
include: [ models.Product ]
}).then(function(prod){
res.json(prod);
}).catch(next);
});
module.exports = router;
product.js (model)
'use strict';
module.exports = function(sequelize, DataTypes) {
var Product = sequelize.define('Product', {
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
libelle: { type: DataTypes.STRING, allowNull: false },
type: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.STRING, allowNull: true },
ean13Code: { type: DataTypes.STRING, unique: true, allowNull: true }
}, {
paranoid: true,
underscored: true,
freezeTableName: true
});
return Product;
};
via O. Lefebvre
No comments:
Post a Comment