我创建一个表来存储用户会话中我将IP地址存储为一个整数,使用这些方法:IP-addresses stored as int results in overflow?Node.js的Sequelize的getter/setter的RangeError
我想说明一个IP领域的getter和setter,以便它可以在IP和int之间自动转换。
不幸的是我得到以下错误,我不知道发生了什么。我一直在试图修复它几个小时,谷歌得到我得不到任何结果:
RangeError: Maximum call stack size exceeded
型号:
model = db.define(name, {
id: {type: Sequelize.STRING, allowNull: false, primaryKey: true},
ipAddress: {type: Sequelize.INTEGER(11).UNSIGNED, allowNull: false},
userAgent: {type: Sequelize.STRING, allowNull: false},
username: {type: Sequelize.STRING, allowNull: false},
password: {type: Sequelize.STRING, allowNull: false},
firstName: {type: Sequelize.STRING, allowNull: false},
lastName: {type: Sequelize.STRING, allowNull: false},
email: {type: Sequelize.STRING}
}, {
getterMethods: {
name: function() { return this.firstName + this.lastName },
ipAddress: function() {
ip = this.getDataValue("ipAddress");
return ((ip >> 24) & 255) + "." + ((ip >> 16) & 255) + "." + ((ip >> 8) & 255) + "." + (ip & 255);
}
},
setterMethods: {
ipAddress: function(ip) {
var parts = ip.split(".");
var ret = 0;
ret += parseInt(parts[0], 10) << 24;
ret += parseInt(parts[1], 10) << 16;
ret += parseInt(parts[2], 10) << 8;
ret += parseInt(parts[3], 10);
return ret;
}
}
});
插入一个IP:
model.findOrCreate({id: sessionId}, {
id: sessionId,
ipAddress: req.ip, // === "192.168.1.79"
userAgent: req.get("user-agent"),
username: "test",
password: "test",
firstName: "first",
lastName: "last",
email: "email"
})
我可以证实, getter/setter代码会根据需要进行转换,但它在Sequelize中无法正常工作。