(Sequelize) Cannot read Property 'title' of Undefined

Asked

Viewed 129 times

0

Hello, I am developing a cart system for Ecommerce, and within the Model Cart, I have made a relationship with User Model and Product Model, so I can access the two items through the Cart. However, it does not recognize the fields of the other models (Product and User). Codes:

Product.js

const Sequelize = require('sequelize')
const Category = require('./Category.js')
const connect = require('./Database.js')

const Product = connect.define('products', {
    
    title: {
        type: Sequelize.STRING(120),
        allowNull: false
    },

    slug: {
        type: Sequelize.STRING,
        allowNull: false
    },

    image: {
        type: Sequelize.STRING,
        allowNull: false
    },

    price: {
        type: Sequelize.INTEGER,
        allowNull: false    
    },

    body: {
        type: Sequelize.TEXT,
        allowNull: false
    }
})

Category.hasMany(Product)
Product.belongsTo(Category)

Product.sync({ force: false })

module.exports = Product

User.js

const Sequelize = require('sequelize')
const connect = require('./Database.js')

const User = connect.define('users', {
    
    name: {
        type: Sequelize.STRING(120),
        allowNull: false
    },

    email: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true
    },

    password: {
        type: Sequelize.STRING,
        allowNull: false
    },

    cellphone: {
        type: Sequelize.STRING(15),
        allowNull: false,
        unique: true
    },

    is_staff: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
    }
})

User.sync({ force: false })

module.exports = User

Cart.js

const Sequelize = require('sequelize')
const Product = require('./Product.js')
const User = require('./User.js')
const connect = require('./Database.js')

const Cart = connect.define('carts', {
    quantity: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 1
    }

})

Cart.belongsTo(Product)
Cart.belongsTo(User)

Cart.sync({ force: true })

module.exports = Cart

Bug Image

  • The error is happening in the information query apparently, you just put the definitions of the models, post the code where the query in the database is made to get help

  • I used the normal findAll() I tried to include it but it made other mistakes.

1 answer

0

The error is happening in the information query apparently, you just put the settings of the models, post the code where the query in the database is made to get help, but there is something you can try.

Sequelize does not include in the query by default the entities mapped within the entity being searched, in the documentation is cited the use of include:

await User.findAll({ include: Task });

Upshot:

[{
  "name": "John Doe",
  "id": 1,
  "tasks": [{
    "name": "A Task",
    "id": 1,
    "userId": 1
  }]
}]

An array can be passed to include property, something like:

await User.findAll({ include: [Task, OutraTabela] });

Information taken from sequelize documentation.

  • I tried to use include, but it gave other errors, "as" error, association...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.