NodeJS:Sequelize定义model,提示Cannot read property 'hasOwnProperty' of undefined

bohe2005 发布于 2018/03/21 17:49
阅读 2K+
收藏 0

环境:win10, vscode 版本v1.21.1, NodeJS + express 4.x + sqlite3 + sequelize

现状:表比较多,需要独立成不同的model,把映射关系独立出来

问题:使用sequelize后,从路由提取到 独立的 js 文件后,报错

第一次错误

第二次更详细的错误

 

相关信息:

(1)控制台开始时的执行信息

问题代码

(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() {
    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
    });
    return PlantUnitModel;
}

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

(3)路由

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: 若启用该段代码,可以获取数据
/* 
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: 若启用该段代码,可以获取数据

/**
 * 查询列表页
 */
router.get('/', function (req, res) {
    res.render('params/plantUnit/list');
});
router.get('/json', function (req, res) {
    var list = [];
    console.log("json");
    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);
    });
});

 

不知道具体应该怎样改,也没查到对应的网页。烦请指点,感谢。

 

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部