我已經在模型文件中定義了一個常量變量,並在向表中插入數據時嘗試訪問此常量變量,然後顯示未定義變量。我使用了sequlaize ORM和nodejs。我想爲狀態定義三個值,如Active = 1,Inactive = 2,Deleted = 3,還是有其他解決方案來定義它。我正在使用續集ORM 4+版本。如何在nodejs中訪問控制器中的常量變量
Model code is here
var Sequalize = require('sequelize');
var sequalize = require('../../config/db_config');
const ACTIVE = '1';
const INACTIVE = '2';
const DELETED = '3';
const User = sequalize.define('user',{
user_id_pk : {
type : Sequelize.INTEGER,
allowNull : false,
primaryKey : true,
autoIncrement : true
},
user_type : Sequelize.INTEGER,
first_name : Sequelize.STRING,
middle_name : Sequelize.STRING,
last_name : Sequelize.STRING,
password : Sequelize.STRING,
email : Sequelize.STRING,
mobile_no : Sequelize.STRING,
is_email_verified : {
type:Sequalize.INTEGER,
defaultValue : 0
},
gender : Sequelize.STRING,
profile_image: Sequelize.STRING,
dob : Sequelize.DATE,
is_active : Sequelize.STRING,
created_date : Sequelize.DATE,
created_by : Sequelize.INTEGER,
updated_date : Sequelize.DATE,
updated_by : Sequelize.INTEGER,
},{
timestamps: false,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: 'user',
createdAt:'created_date',
updatedAt:'updated_date'
});
User.sync({force:false}).then(() => {
console.log('Table is created!');
}).catch(err => {
console.log('An error occur when table is created!');
});
module.exports = User;
Controller code is here
userController.saveUser = function(req,res){
console.log(user.ACTIVE);return false;
user.findOrCreate({
where:{
first_name: req.body.first_name,
email: req.body.email,
boltt_code: req.body.boltt_code
},
defaults:{
first_name: req.body.fihhrst_name,
middle_name : 'Rajeev',
last_name : 'Varshney',
email: req.body.email,
boltt_code: req.body.boltt_code,
is_active : '1',
}
})
.spread(function(user, created) {
var msg = '';
if(created){
msg = 'User inserted successfully!';
}else{
msg = 'User already exist!';
}
res.status(200).send({error : false,message : msg2, data : user });
})
.catch((err) => {
console.log('Oops something wrong! ' + err);
});
};
這個問題與MySQL有什麼關係? – axiac
我確定你使用它,但在這個問題中我看不到與MySQL有關的任何東西。你應該刪除MySQL標籤並添加[tag:sequelize.js]標籤。如果仔細觀察,庫的名稱是「Sequelize」,而不是「Sequalize」,因爲您拼錯了它。然後你想知道爲什麼代碼不起作用... – axiac
我的意思是,在代碼中。它充滿了'Sequalize.INTEGER'。即使[Google知道](https://www.google.ro/search?q=Sequalize.INTEGER)它應該是['Sequelize.INTEGER'](http://docs.sequelizejs.com) – axiac