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
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
– João Victor Souza
I used the normal findAll() I tried to include it but it made other mistakes.
– Carlos