0

Good evening I'm getting this error in my express JS application and not sure how to resolve it. I'm using an existing mySQL db and trying to retrieve items from my tbl_person table in myDB database. I'm pretty new to this, I'm not too sure what I'm doing incorrect. Some of the examples I'm seeing online is not entirely clear so I need some help.

Here is my sample code:

db.config.js

module.exports = {
    HOST: "127.0.0.1",
    USER: "root",
    PASSWORD: "password",
    DB: "myDB"
}; 

person.model.js

module.exports = (sequelize, DataTypes) => {
    const Person = sequelize.define('tbl_person', {
        personID: {
            type: DataTypes.STRING(36),
            field: 'personId',
            allowNull: false,
            primaryKey: true,            
        },
        
        accountname: {
            type: DataTypes.STRING(50),
            field: 'accountname',
            allowNull: true,
        },
    password: {
            type: DataTypes.STRING(100),
            field: 'password',
            allowNull: true,
        },
        nameprefix: {
            type: DataTypes.STRING(20),
            field: 'nameprefix',
            allowNull: true,
        },
        firstname: {
            type: DataTypes.STRING(50),
            field: 'firstname',
            allowNull: true,
        },
        middlename: {
            type: DataTypes.STRING(50),
            field: 'middlename',
            allowNull: true,
        },
        lastname: {
            type: DataTypes.STRING(50),
            field: 'lastname',
            allowNull: true,
        },
   })
    return Person;
};

person.controller.js

const person = require("../models/person.model.js").Person;

module.exports = {
    
    getPersons(req, res) {
        person.findAll({
            where: { isActive : 1 },
            
        }).then(person => {
            res.status(201).json({
                person: person,
                success: true,
                message: "get person request successful."
            });
        }).catch(error => {
            console.error("get person request failed: ", error);
            res.status(500).send({
                success: false,
                message: "get person request failed: " + error
            });
        })
    }


};

index.js

const person = require("../controllers/person.controller.js");

module.exports = app => {
    app.get("/api", (req, res) =>
        res.status(200).send({
            message: "Welcome to my API!",
        })
    );

    // ========================= Person Routes ========================= //
    app.get("/api/person/get", person.getPersons);



};

server.js

const express = require("express");
const bodyParser = require("body-parser");
const app = express();

require("./routes/index.js")(app);
app.get("*", (req, res) => {
    res.status(404).send({
        status: false,
        message: "No matching route!"
    });
});

//PORTS
const port = process.env.PORT || 3000;

// set port, listen for requests
app.listen(port, () => {
  console.log(`Server is running on port ${port}.`);
});
  • From where do you get the value `sequelize, DataTypes` in the file:`person.model.js`? – Harshana Dec 01 '20 at 01:48
  • 1
    It's in my config.js file and thanks for pointing that out because that may have solved my problem because I missed it. I'll report back in a few minutes. – KenNotAnthony Dec 01 '20 at 02:00

1 Answers1

1

You should register all models and their associations before using them. And you should use them either from Sequelize instance or from your object holding all registered models.

See my answer here about how to register all models and associations.

Anatoly
  • 20,799
  • 3
  • 28
  • 42