如何在nodejs中访问控制器中的常量变量

时间:2017-07-28 12:51:20

标签: node.js sequelize.js

我在模型文件中定义了一个常量变量,并尝试在将数据插入表时访问此常量变量,然后显示未定义。我正在使用sequlaize ORM和nodejs。我想为状态定义三个值,如Active = 1,Inactive = 2,Deleted = 3,还是有任何其他解决方案来定义它。我正在使用sequelize ORM 4+版本。

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);
    });
};

3 个答案:

答案 0 :(得分:1)

如果要在同一文件中使用这些变量,只需声明它们并使用它即可。 如果要从其他文件访问它们,则必须导出变量:

const ACTIVE = '1';
const INACTIVE = '2';
const DELETED = '3';
exports.ACTIVE = ACTIVE;
exports.INACTIVE = INACTIVE;
exports.DELETED = DELETED;

现在您可以对文件进行要求并访问变量。

答案 1 :(得分:0)

ModelCore.js 文件中,不仅导出用户,还导出常量:

// ModelCore.js
var Sequalize = require('sequelize');
var sequalize = require('../../config/db_config');

const ACTIVE = '1';
const INACTIVE = '2';
const DELETED = '3';

const User = sequalize.define('user', {...});

module.exports = {
    User,
    CODES: {
        ACTIVE,
        INACTIVE,
        DELETED
    }
}

现在将其导入 ControllerCode.js

// ControllerCode.js
const modelCore = require('./path/to/ModelCore');

const user = modelCore.User;
const {CODES} = modelCore;

//Using codes now should work
console.log(CODES.ACTIVE);
console.log(CODES.INACTIVE);
console.log(CODES.DELETED);

// Rest of your Controller code...

答案 2 :(得分:0)

We can initialize constant variable to model instance after creating the model instance. 

const ACTIVE = 1;
const INACTIVE = 2;
const DELETED = 3;

const user = sequelize.define('user',{..},{

});

User.ACTIVE = ACTIVE;
User.INACTIVE = INACTIVE;
User.DELETED = DELETED;

module.exports = User;
相关问题