It was not finding my config.json that I was trying to instance it with .sequelizerc so I just let the migrations on this file and created my config.json. It is fine and the Tables and database are created just fine. But when I try to fetch my API, I am getting an error.
Here are my configurations:
My routes.js
const express = require('express');
const UserController = require('../src/controllers/UserController');
const routes = express.Router();
// routes.get('/', (req, res) => {
// return res.json({ message: 'Hello World' });
// })
routes.get('/users', UserController.index);
routes.post('/users', UserController.store);
module.exports = routes;
My Server.js
const express = require('express');
const routes = require('./routes');
require('../config/config.json');
const app = express();
app.use(express.json());
app.use(routes);
app.listen(3333);
My Controller to get or add user:
const User = require('../models/User');
module.exports = {
async index(req, res) {
const users = await User.findAll();
return res.json(users);
},
async store(req, res) {
const { name, email } = req.body;
const user = await User.create({ name, email });
return res.json(user);
}
}
My Model
const { Model } = require('sequelize');
class User extends Model {
static init(sequelize){
super.init({
name: DataTypes.STRING,
email: DataTypes.STRING,
}, {
sequelize
});
}
}
module.exports = User;
I am getting this error message:
if (this.constructor.primaryKeyAttributes.length) {
^
TypeError: Cannot read properties of undefined (reading 'length')