I’m making a project with Sequelize and I’m stuck on this step. The problem is that when i load http://localhost:3000/hygro and try to run hygro.js i get this error in return
TypeError: hygroData.findALL is not a function
at C:Userseddyghygrometerrouteshygro.js:5:19
at Layer.handle [as handle_request] (C:Userseddyghygrometernode_modulesexpresslibrouterlayer.js:95:5)
at next (C:Userseddyghygrometernode_modulesexpresslibrouterroute.js:144:13)
at Route.dispatch (C:Userseddyghygrometernode_modulesexpresslibrouterroute.js:114:3)
at Layer.handle [as handle_request] (C:Userseddyghygrometernode_modulesexpresslibrouterlayer.js:95:5)
at C:Userseddyghygrometernode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (C:Userseddyghygrometernode_modulesexpresslibrouterindex.js:346:12)
at next (C:Userseddyghygrometernode_modulesexpresslibrouterindex.js:280:10)
at expressInit (C:Userseddyghygrometernode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:Userseddyghygrometernode_modulesexpresslibrouterlayer.js:95:5)
im really lost on what to do, im newer to javascript. Im working on this for my classes and its just been very furstrating for me
heres my structure:
(i didnt include my libs folder it included setup things, im not sure if its necessary, if needed i can show it.)
package.json
{
"name": "hygrometer",
"version": "1.0.0",
"description": "This will be the api for my hyrgometer data",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "node test.js"
},
"author": "Eddy Gallegos",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"consign": "^0.1.6",
"express": "^4.18.2",
"sequelize": "^6.37.1",
"sqlite3": "^5.1.7"
}
}
db.js
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
let db = null;
module.exports = app => {
if (!db){
const config = app.libs.config;
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.params
);
db = {
sequelize,
Sequelize,
models: {}
};
const dir = path.join(__dirname,"models");
fs.readdirSync(dir).forEach(file => {
const modelDir = path.join(dir, file);
const model = require(modelDir)(sequelize,Sequelize.DataTypes);
db.models[model.name]= model;
});
Object.keys(db.models).forEach(key => {
db.models[key].options.classMethods.associate(db.models);
});
}
return db;
};
app.js
const express = require("express");
const consign = require ("consign");
const app = express();
consign()
.include("libs/config.js")
.then("db.js")
.then("libs/middlewares.js")
.then("routes")
.then("libs/boot.js")
.into(app);
routes/hygro.js
module.exports = app => {
const hygroData = app.db.models.Readings;
app.get("/hygro", (req,res) => {
hygroData.findALL({}).then(data => {
res.json({data:data});
})
});
}
models/locations.js
module.exports = (sequelize, DataType) => {
const Locations = sequelize.define("Locations", {
id: {
type: DataType.INTEGER ,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataType.TEXT,
unique: true,
allowNull: false,
validate: {
notEmpty: true
}
},
street: {
type: DataType.TEXT,
allowNull: false,
validate : {
notEmpty: true
}
},
city: {
type: DataType.TEXT,
allowNull: false,
validate:{
notEmpty: true
}
},
state: {
type: DataType.TEXT,
allowNull: false,
validate:{
notEmpty: true
}
},
zip: {
type: DataType.TEXT,
allowNull: false,
validate:{
notEmpty: true
}
}
}, {
classMethods: {
associate: (models) => {
Locations.hasMany(models.Readings);
}
}
});
return Locations;
}
models/readings.js
module.exports = (sequelize, DataType) => {
const Readings = sequelize.define("Readings",{
id: {
type: DataType.INTEGER ,
primaryKey: true,
autoIncrement: true
},
date: {
type: DataType.TEXT,
allowNull: false,
validate: {
notEmpty: true
}
},
temperature: {
type: DataType.INTEGER,
allowNull: false,
validate : {
notEmpty: true
}
},
humidity: {
type: DataType.INTEGER,
allowNull: false,
validate:{
notEmpty: true
}
}
}, {
classMethods: {
associate: (models) => {
Readings.belongsTo(models.Locations);
}
}
});
return Readings;
}