fru*_*cup 4 javascript node.js express sequelize.js
我正在创建一个表来存储用户会话。我将使用以下方法将 IP 地址存储为整数: IP 地址存储为整数存储为 int 的 IP 地址会导致溢出?
我想为 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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
插入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"
})
Run Code Online (Sandbox Code Playgroud)
我可以确认 getter/setter 代码确实按需要进行了转换,但它在 Sequelize 中无法正常运行。
我也遇到了 Sequelize 的这个问题。我反复阅读官方文档,在互联网上搜索我知道的所有地方,最后我终于到了这里。一旦我发现了你的问题,没有答案,我决定搜索 Sequelize 本身的源代码。果然,我找到了答案!
如果您查看sequelize/test/dao-factory.test.jsSequelize 源代码中的文件,您会发现以下代码隐藏在测试用例中:
setterMethods: {
price1: function(v) { this.setDataValue('price1', v * 100) }
},
Run Code Online (Sandbox Code Playgroud)
我复制了上面的语法,在我自己的 setter 方法中使用它,它有效!
他们真的应该更新 sequelize 网站上的文档.. 嗯.. 也许我应该考虑帮助他们解决这个问题?不管怎样,祝你好运!
| 归档时间: |
|
| 查看次数: |
2396 次 |
| 最近记录: |