function handler(e){
const el = e.target;
const activeBtn = el.querySelector('.btn_active')
const activePane = el.querySelector('.pane_active')
// next logic
}
И если я буду искать не по хешу, а по обыкновенному id, то хеш я не получу, мне придётся делать функцию, которая будет преобразовывать id в хеш?
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
console.log(this.get("id"))
return hashids.encode(this.get("id"));
}
},
из-за чего может быть это?- это виртуальная колонка. Её следует удалять перед запросом в бд, у вас вероятно нет колонки hash в бд
const Sequelize = require('sequelize');
const config = require('../../config')
const Hashids = require('hashids');
const hashids = new Hashids('', 0, config.hashids.string);
const ads = sequelize.define('ads', {
hash: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ["id"]),
get: function() {
return hashids.encode(this.get("id"));
}
},
user_panel_id: {
type: Sequelize.INTEGER,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true,
hooks: {
beforeFind: pkHashToNumber,
beforeDestroy: pkHashToNumber,
beforeUpdate: pkHashToNumber
}
})
ads.sync({force: false})
module.exports = ads;
const hash = "MDFPHX";
const user = User.find({where: { hash: hash }});
console.log(user.user_id); // 1
function pkHashToNumber(options) {
if (!options.where) {
return;
}
let pk = options.where.user_id;
if (!pk) return;
if (typeof pk === 'string' && isNaN(pk)) {
options.where.user_id= hashids.decode(pk);
}
}
const Model = sequelize.define('task', {
hash: {
type: new DataTypes.VIRTUAL(DataTypes.STRING, ["user_id"]),
get: function() {
return hashids.encode(this.get("user_id"));
}
},
},
hooks: {
beforeFind: pkHashToNumber,
beforeDestroy: pkHashToNumber,
beforeUpdate: pkHashToNumber
})