sequelize:提示Cannot read property 'hasOwnProperty' of undefined

发布于 2022-09-06 21:12:35 字数 5895 浏览 17 评论 0

问题:
在nodejs + express4.x + sqlite3项目中,使用sequelize,在路由文件中定义model,可以查询到数据。将model定义提取出来,再查询时,提示
TypeError: Cannot read property 'hasOwnProperty' of undefined

代码
(1)sequelizeConfig

var fs = require("fs");
var file = "./public/db/sdpmodelbuild.db";
var exists = fs.existsSync(file);
if(!exists) {
    console.log("Creating DB file.");
    fs.openSync(file, "w");
}

var Sequelize = require('sequelize');
var sequelizeConn = new Sequelize("database", "username", "password",{
    host: 'localhost',
    dialect:'sqlite',
    pool: {  
        max: 5,  
        min: 0,  
        idle: 10000  
      },  
    storage:file
});
 
//测试数据库链接
sequelizeConn.authenticate().then(function() {
    console.log("数据库连接成功");
}).catch(function(err) {
    //数据库连接失败时打印输出
    console.error("数据库连接失败,错误:" + err);
    throw err;
});

module.exports.sequelizeConn = sequelizeConn;
module.exports.Sequelize = Sequelize;

(2)PlantUnitModel

var sequelizeConfig = require("../config/sequelizeConfig");

var Sequelize = sequelizeConfig.Sequelize;
var sequelizeConn = sequelizeConfig.sequelizeConn;

var PlantUnitBean = sequelizeConn.define("PlantUnitBean",{
    ID: {type: Sequelize.STRING, allowNull: false, primaryKey: true, unique: true},
    PLANTCODE: {type: Sequelize.STRING},
    PLANTNAME: {type: Sequelize.STRING},
    UNITCODE: {type: Sequelize.STRING},
    UNITNAME: {type: Sequelize.STRING},
    UNITCN: {type: Sequelize.STRING},
    UNITS: {type: Sequelize.STRING},
    ISDELETE: {type: Sequelize.INTEGER},
    BLOCID: {type: Sequelize.STRING},
},{
    tableName: 'PAR_UNIT_TB',
    // 自定义表名
    freezeTableName: true,
    // 是否需要增加createdAt、updatedAt、deletedAt字段
    timestamps: true,
    // 不需要createdAt字段
    createdAt: false,
    // 将updatedAt字段改个名
    //updatedAt: 'modifiedOn',
    updatedAt: false,
    // 将deletedAt字段改名
    // 同时需要设置paranoid为true(此种模式下,删除数据时不会进行物理删除,而是设置deletedAt为当前时间
    //deletedAt: 'deletedOn',
    deletedAt: false,
    paranoid: true
});

function getPlantUnitModel() {
    console.log("model sequelizeConn==>" + sequelizeConn + ", Sequelize" + Sequelize.STRING);
    var PlantUnitModel = sequelizeConn.define("PlantUnitModel",{
        ID: {type: Sequelize.STRING, allowNull: false, primaryKey: true, unique: true},
        PLANTCODE: {type: Sequelize.STRING},
        PLANTNAME: {type: Sequelize.STRING},
        UNITCODE: {type: Sequelize.STRING},
        UNITNAME: {type: Sequelize.STRING},
        UNITCN: {type: Sequelize.STRING},
        UNITS: {type: Sequelize.STRING},
        ISDELETE: {type: Sequelize.INTEGER},
        BLOCID: {type: Sequelize.STRING},
    },{
        tableName: 'PAR_UNIT_TB',
        // 自定义表名
        freezeTableName: true,
        // 是否需要增加createdAt、updatedAt、deletedAt字段
        timestamps: true,
        // 不需要createdAt字段
        createdAt: false,
        // 将updatedAt字段改个名
        //updatedAt: 'modifiedOn',
        updatedAt: false,
        // 将deletedAt字段改名
        // 同时需要设置paranoid为true(此种模式下,删除数据时不会进行物理删除,而是设置deletedAt为当前时间
        //deletedAt: 'deletedOn',
        deletedAt: false,
        paranoid: true
    });
    console.log("model PlantUnitModel==>" + PlantUnitModel);
    return PlantUnitModel;
}

module.exports.PlantUnitBean = PlantUnitBean;
module.exports.getPlantUnitModel = getPlantUnitModel;

(3)plantUnit

var express = require('express');
var router = express.Router();
//引入数据库包
var dbUtil = require("../util/dbUtil.js");
var PlantUnitModel = require("../model/PlantUnitModel");
var test = require("../model/PlantUnitModel").PlantUnitBean;

// start: 下列代码启用,不取 model 文件,可以获取到数据
/*
var fs = require("fs");
var file = "./public/db/sdpmodelbuild.db";
var exists = fs.existsSync(file);
if(!exists) {
    console.log("Creating DB file.");
    fs.openSync(file, "w");
}

var Sequelize = require('sequelize');
var sequelizeConn = new Sequelize("database","username","password",{
    host: 'localhost',
    dialect:'sqlite',
    pool: {  
        max: 5,  
        min: 0,  
        idle: 10000  
      },  
    storage:file
});

var PlantUnitBean = sequelizeConn.define("PlantUnitBean",{
    ID: {type: Sequelize.STRING, allowNull: false, primaryKey: true, unique: true},
    PLANTCODE: {type: Sequelize.STRING},
    PLANTNAME: {type: Sequelize.STRING},
    UNITCODE: {type: Sequelize.STRING},
    UNITNAME: {type: Sequelize.STRING},
    UNITCN: {type: Sequelize.STRING},
    UNITS: {type: Sequelize.STRING},
    ISDELETE: {type: Sequelize.INTEGER},
    BLOCID: {type: Sequelize.STRING},
},{
    tableName: 'PAR_UNIT_TB',
    // 自定义表名
    freezeTableName: true,
    // 是否需要增加createdAt、updatedAt、deletedAt字段
    timestamps: false
});
PlantUnitBean.sync();
*/
// end: 下列代码启用,不取 model 文件,可以获取到数据

/**
 * 查询列表页
 */
router.get('/', function (req, res) {
    res.render('params/plantUnit/list');
});
router.get('/json', function (req, res) {
    var list = [];
    PlantUnitModel.getPlantUnitModel().findAll({
        "where": {
            "ISDELETE": {$ne: 1}
        }
    }).then(function(result) {
        for (var i = 0; i < result.length; i++) { 
            list.push(result[i].toJSON());
        }
        console.log("list:" + list);
        if (list) {
            res.json({errno:0, title: '', data: list, s_name: ""});
        } else {
            res.json({errno:0, title: '', data: [], s_name: ""});
        }
    }).catch((err)=>{
        console.log("json err ==>" + err);
    });
});

错误信息
图片描述

已查找
https://github.com/caiya/node...
https://stackoverflow.com/que...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2022-09-13 21:12:35

下列代码启用,不取 model 文件,可以获取到数据
不明白这部分是什么意思
catch证明PlantUnitModel.getPlantUnitModel().findAll这个Promise执行了,所有错误应该是$nesequelize为了避免sql注入默认不能把$ne这类操作符当字符串处理。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文