nicholasf / ectypes-sequelize.js

An ectypes.js strategy for Sequelize.js
2 stars 1 forks source link

hook with sequelize causing error #1

Open nicholasf opened 12 years ago

nicholasf commented 12 years ago
        , Broker: {
            name: function(){ return Faker.Name.findName() }
            , _hooks: [{'create a user': function(broker, funcName){
                if (funcName === "create"){
                    ectypes.User.create().success(function(user){
                        console.log(" - - - - - ", broker);
                        user.broker_id = broker.id;
                        user.save();
                    })
                }
            }
        }]

the stacktrace:


/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/mysql/lib/client.js:187
  val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
            ^
TypeError: Object function () {
    switch(Helpers.randomNumber(2))
    {
    case 0:
        return Helpers.randomize(definitions.first_name());
        break;
    case 1:
        return Helpers.randomize(definitions.first_name()) + Helpers.randomize([".", "_"]) + Helpers.randomize(definitions.last_name()) ;
        break;
    }
} has no method 'replace'
    at Client.escape (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/mysql/lib/client.js:187:13)
    at Object.module.exports.escape (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/utils.js:38:19)
    at module.exports.QueryGenerator.insertQuery.replacements.values (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dialects/mysql/query-generator.js:138:24)
    at Array.map (native)
    at Array.<anonymous> (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sugar/release/1.3/sugar-1.3-full.development.js:104:25)
    at Object.module.exports.QueryGenerator.insertQuery (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dialects/mysql/query-generator.js:137:47)
    at module.exports.QueryInterface.insert (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/query-interface.js:181:35)
    at module.exports.DAO.save (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dao.js:107:34)
    at module.exports.DAOFactory.create (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dao-factory.js:193:31)
    at Object.exports.create (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/ectypes-sequelize/lib/ectypes-sequelize.js:17:26)
Siridivi commented 11 years ago

I have this same problem. I haven't been able to fix it, but here is what I've found.

I installed the util.js library and used it to inspect the val object by adding this line prior to line 187. console.log(util.inspect(val, 1, true));

val is a function object, not a string object. Function objects do not have the replace method as part of their parent class so do not inherit it, string objects do. Even though there is a bit of type checking in the Client.protoype.escape method where this problem occurs, at the time the replace method is used, val is not of type string, and thus the error is generated.

I tried many ways to fix this (mainly involving hunting down where the replace function is invoked and casting the val argument to a string, but that broke the escaping function and thus the corresponding query as well), but I am a neophyte in Javascript and am learning node.js as a way to learn Javascript and my solutions didn't work. Perhaps you will fair better.

Hope this helps.

nicholasf commented 11 years ago

Hi,

Ectypes has changed quite a bit and I have stopped using Sequelize (we wrote an ORM ourselves - https://github.com/moneytribeaustralia/downstairs.js ).

If you want to fix ectypes-sequelize have a look at ectypes-downstairs: https://github.com/moneytribeaustralia/ectypes-downstairs.js/blob/master/lib/ectypes-downstairs.js . It will probably be very simple to upgrade ectypes-sequelize to use the latest version of ectypes and have things work for you.

nicholasf commented 11 years ago

Btw, hooks have been dropped in ectypes now. Instead there are 'befores' which are more powerful.