A
A
artr_lr2019-02-20 20:45:51
JavaScript
artr_lr, 2019-02-20 20:45:51

Sequelize, PUG and length?

In general, my roof is already starting to boil from this ...
There is a database, it seems like there are three photos in it.
We have a model (I specifically insert all the code from the file, because it is the most suspect):

//  /models/photo.js

const Sequelize = require('sequelize');
const db = require('../config/database');

const Photo = db.define('photo', {
  photo_id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER
  },
  alt: {
    type: Sequelize.STRING
  },
  description: {
    type: Sequelize.STRING
  },
  image: {
    type: Sequelize.STRING
  },
},{
  timestamps: false,
  freezeTableName: true,
});

module.exports = Photo;

Router:
router.get('/photo', (req, res) => {
  Photo.findAll()
    .then(photos => {
      console.log(photos.length); // выводит 3!
      res.render('index', {
        photos: photos
      });
    })
    .catch((err) => console.log(`Error: ${err}`));
});

and the render template itself
each key in photos
      p= key.image

As a result, we get the error "TypeError: Cannot read property 'length' of undefined" in the terminal. The data is displayed, but due to an error in the terminal, there is no further transformation of CSS and other things (I use webpack-dev-middleware and webpack-hot-middleware).
Googled the whole stackoverflow) everyone had this problem if the data type was specified in the model as NUMBER, which does not exist, but I have STRING and INTEGER, according to the documentation!
Further they wrote that pug is not very friendly with asynchronous rendering, but it also renders this
| #{photos}
on the screen and displays without errors
[object SequelizeInstance: photo], [object SequelizeInstance: photo], [object SequelizeInstance: photo]
In general, I don’t know what and do guys....

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question