我創建一個表來存儲用戶會話中我將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中無法正常工作。