I’m starting out with Sequelize and got kind of stuck right there. I try to connect my controller, route, model and stuff and I get this error when trying to access my route (localhost:3000/api/carousel) :
TypeError: Cannot read properties of undefined (reading 'findAll')
at getAllCarouselItems (C:UserstheobOneDriveDocumentsDEVle_burguignonbackcontrollerscarousel-item_controller.js:6:47)
at Layer.handle [as handle_request] (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterroute.js:144:13)
at Route.dispatch (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterroute.js:114:3)
at Layer.handle [as handle_request] (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterlayer.js:95:5)
at C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterindex.js:346:12)
at next (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterindex.js:280:10)
at Function.handle (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterindex.js:175:3)
at router (C:UserstheobOneDriveDocumentsDEVle_burguignonbacknode_modulesexpresslibrouterindex.js:47:12)
And here’s my controller where the findAll method just won’t work:
const CarouselItem = require('../models/carousel-item');
const db = require('../config/sequelize');
async function getAllCarouselItems(req, res) {
try {
const carouselItems = await db.CarouselItem.findAll();
res.json(carouselItems);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Server Error' });
}
}
module.exports = { getAllCarouselItems };
Is there something I’ve been missing?
I tried using console logs, searching online using these errors, even asking chat gpt for help but being a beginner with this technology, I get confused and am not sure where to look at.
2
Answers
While I was still trying things out I might have figured it out but can you tell me if this was really the reason so I can understand?
So I figured I'd add a sequelize.authenticate to test my connexion to the database and it all started working. Had I misunderstood what its purpose was and so is it needed for the whole thing to actually work?
So basically I added :
in my app.js, and it worked, even tho I am not sure if I was mistaken about authenticate() being something you use to test the connexion.
As for db, I removed it.