鍍金池/ 問答/數(shù)據(jù)庫  HTML/ sequelize:提示Cannot read property 'hasOwn

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

問題:
在nodejs + express4.x + sqlite3項(xiàng)目中,使用sequelize,在路由文件中定義model,可以查詢到數(shù)據(jù)。將model定義提取出來,再查詢時(shí),提示
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
});
 
//測試數(shù)據(jù)庫鏈接
sequelizeConn.authenticate().then(function() {
    console.log("數(shù)據(jù)庫連接成功");
}).catch(function(err) {
    //數(shù)據(jù)庫連接失敗時(shí)打印輸出
    console.error("數(shù)據(jù)庫連接失敗,錯誤:" + 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字段改名
    // 同時(shí)需要設(shè)置paranoid為true(此種模式下,刪除數(shù)據(jù)時(shí)不會進(jìn)行物理刪除,而是設(shè)置deletedAt為當(dāng)前時(shí)間
    //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字段改名
        // 同時(shí)需要設(shè)置paranoid為true(此種模式下,刪除數(shù)據(jù)時(shí)不會進(jìn)行物理刪除,而是設(shè)置deletedAt為當(dāng)前時(shí)間
        //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();
//引入數(shù)據(jù)庫包
var dbUtil = require("../util/dbUtil.js");
var PlantUnitModel = require("../model/PlantUnitModel");
var test = require("../model/PlantUnitModel").PlantUnitBean;

// start: 下列代碼啟用,不取 model 文件,可以獲取到數(shù)據(jù)
/*
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 文件,可以獲取到數(shù)據(jù)

/**
 * 查詢列表頁
 */
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...

回答
編輯回答
菊外人
下列代碼啟用,不取 model 文件,可以獲取到數(shù)據(jù)
不明白這部分是什么意思
catch證明PlantUnitModel.getPlantUnitModel().findAll這個Promise執(zhí)行了,所有錯誤應(yīng)該是$nesequelize為了避免sql注入默認(rèn)不能把$ne這類操作符當(dāng)字符串處理。
2017年11月1日 10:08