2013-08-05 31 views
4

我创建一个表来存储用户会话中我将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中无法正常工作。

回答

4

我也遇到了Sequelize这个问题。我正在阅读并重新阅读官方文档,搜索我知道在互联网上搜索的任何地方,最后我终于在这里结束了。一旦我找到你的问题,没有答案,我决定搜索Sequelize本身的源代码。果然,我找到了答案!

如果您在Sequelize的源代码查看文件sequelize/test/dao-factory.test.js,你会发现一个代码的测试用例里面潜伏以下位:

setterMethods: { 
    price1: function(v) { this.setDataValue('price1', v * 100) } 
}, 

我复制上面的语法,使用它在我自己的二传手方法,它的工作原理!

他们真的应该更新续集网站上的文档..嗯..也许我应该考虑帮助他们呢?任何方式,祝你好运!

1

我不知道是否有人仍在运行此错误,但如果您遇到此问题,我将如何解决此问题。

我也有类似的情况,我有它一直在寻找这样一个的usermodel对象:

var UserModel = sequelize.define("users", { 
    ID: { 
     type: Sequelize.INTEGER, 
     field: "ID", 
     primaryKey: true, 
     autoIncrement: true 
    }, 
    Username: { 
     type: Sequelize.STRING, 
     field: "Username" 
    }, 
    Password: { 
     type: Sequelize.STRING, 
     field: "Password" 
    }, 
    Sector: { 
     type: Sequelize.INTEGER, 
     field: "Sector" 
    }, 
    SubSector: { 
     type: Sequelize.INTEGER, 
     field: "SubSector" 
    }, 
    Email: { 
     type: Sequelize.STRING, 
     field: "email" 
    }, 
    Status: { 
     type: Sequelize.INTEGER, 
     field: "status" 
    } 
} 

我的getter和setter方法是:

{ 
    getterMethods: { 
     Sector: function() { 
      return this.Sector; 
     }, 
     Status: function() { 
      return this.Status; 
     } 
    }, 
    setterMethods: { 
     Sector: function(val) { 
      this.setDataValue('Sector',val); 
     }, 
     Status: function(val) { 
      this.setDataValue('Status',val); 
     } 
    } 
}); 

当然,我有堆栈错误。

为了解决这个问题,我只是改变了我的getter和setter方法,以这样的:

{ 
    getterMethods: { 
     currSector: function() { 
      return this.Sector; 
     }, 
     currStatus: function() { 
      return this.Status; 
     } 
    }, 
    setterMethods: { 
     newSector: function(val) { 
      this.setDataValue('Sector',val); 
     }, 
     newStatus: function(val) { 
      this.setDataValue('Status',val); 
     } 
    } 
}); 

,一切都奇迹般地好,尽管许多在线的例子我见过的人建议提供相同的方法setters/getters的名称作为字段。

因此,简而言之,更改为setter和getters的名称,以使其名称与任何定义的字段都不匹配,从而解决了我的问题。良好的做法?我不确定,但它解决了我的问题。

1

我也有类似的情况,我以这种方式

const ProductImages = db.define('product_images', { 
    product_image_id: {type: Sequelize.INTEGER, primaryKey: true, 
    autoIncrement: true}, 
    url: Sequelize.TEXT, 
    product_id: Sequelize.INTEGER, 
    is_primary: Sequelize.INTEGER, 
}, { 
    timestamps: false, 

getterMethods: { 
    is_primary() { 
     return parseInt(this.dataValues.is_primary) 
    }, 

}, 
scopes: { 

    byDefault() { 
     return { 
      include: [{ 
       model: ProductDescriptions, as: 
        'product_description', where: {language_id: 1} 
      }] 
     }; 
     }, 

    } 

}) 

module.exports = ProductImages; 

解决当我打电话this.davaValues.column_name它不叫recoursive getterMethod该列