简体   繁体   English

无法读取节点/express JS 中未定义的属性 FindAll(),现有 mySQL DB

[英]cannot read property FindAll() of undefined in node/express JS, existing mySQL DB

Good evening I'm getting this error in my express JS application and not sure how to resolve it.晚上好,我在我的 express JS 应用程序中遇到了这个错误,不知道如何解决它。 I'm using an existing mySQL db and trying to retrieve items from my tbl_person table in myDB database.我正在使用现有的 mySQL 数据库并尝试从 myDB 数据库中的 tbl_person 表中检索项目。 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 db.config.js

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

person.model.js 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 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 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服务器.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}.`);
});

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.您应该从 Sequelize 实例或持有所有注册模型的 object 使用它们。

See my answer here about how to register all models and associations.请参阅我的回答了解如何注册所有模型和关联。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sequelize.js/Node.js/Express.js:Tasks.findAll() 返回一个 TypeError:Cannot read property 'findAll' of undefined - Sequelize.js/Node.js/Express.js: Tasks.findAll()returns a TypeError:Cannot read property 'findAll' of undefined 无法读取未定义的 sequelize express JS 的属性“findAll” - Cannot read property 'findAll' of undefined sequelize express JS Node.js和mongodb:TypeError:无法读取未定义的属性'findAll' - Node.js and mongodb: TypeError: Cannot read property 'findAll' of undefined 类型错误:无法读取未定义的属性“findAll” - Node.js + Postgresql - TypeError: Cannot read property 'findAll' of undefined - Node.js + Postgresql node.js、express.js 和 mysql - 无法读取未定义的属性“状态” - node.js, express.js, and mysql - Cannot read property 'status' of undefined 无法读取 undefined sequelize express JS 的属性“findAll”并且 req.body 是一个空对象 - Cannot read property 'findAll' of undefined sequelize express JS and req.body is an empty object 类型错误:无法读取未定义的属性“forEach”(节点 js、stripe、express) - TypeError: Cannot read property 'forEach' of undefined (node js, stripe, express) 无法读取未定义的属性'findAll' - Cannot read property 'findAll' of undefined 错误无法使用MySQL使用node.js读取未定义的属性 - Error cannot read property of undefined using node.js with mysql Node.js MySQL错误无法读取未定义的属性? - Node.js mysql error cannot read property of undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM