我搜索了几个小时而没有解决这个问题。我是Node.Js的初学者,我目前正在将它与Express以及Router和Sequelize一起用于学校项目。
上下文
我有一个带有表“products”和两行的MySQL数据库: http://i.imgur.com/U5Lqtpo.png
我想显示每个SQL行的每个id,libelle,type,description和ean13Code。
问题:
我无法将“findAll()”查询的结果传递到Pug模板中。我经常遇到这个错误: 无法读取未定义的属性“长度” 。
代码示例:
以下是代码示例。我希望有人能够知道发生了什么,因为我不知道为什么它不起作用。
如果我忘记提及重要的事情,请告诉我。
'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;
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;
"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;
'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;
};
{{1}}
答案 0 :(得分:0)
从评论中:
似乎Sequelize不喜欢我在产品模型中获得的回报。我不得不做这样的事情:return sequelize.define([...]);而不是var Product = sequelize.define([...]);我不知道为什么,但是现在可以了。但是,我不能再直接将BelongsToMany关联到模型中了,这很糟糕。