sequelize:提示Cannot read property 'hasOwnProperty' of undefined
问题:
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)