mongoosejs / kareem

Next-generation take on pre/post function hooks
Apache License 2.0
82 stars 8 forks source link

Kareem prevent me from adding custom method to Object.prototype #7

Closed menemsalama closed 7 years ago

menemsalama commented 8 years ago

when I'm trying to add a custom method to Object.prototype I got this error:

TypeError: this._pres[key].slice is not a function at Kareem.clone (MYPATH\kareem\index.js:303:36)

vkarpov15 commented 8 years ago

Can you provide a more thorough code sample?

menemsalama commented 8 years ago

Here in this script https://github.com/menemslama/drello/blob/master/sockets.js if I wrote something like that exports.emit = (function () { Object.prototype.emit = function () { console.log("Not working, and it will throw an Error in the terminal"); }; return emit; })();

I will get this error in my terminal from "kareem"

$ node app Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal Not working, and it will throw an Error in the terminal {MYPATH}\drello\node_modules\kareem\index.js:303 n._pres[key] = this._pres[key].slice(); ^

TypeError: this._pres[key].slice is not a function at Kareem.clone ({MYPATH}\drello\node_modules\kareem\index. js:303:36) at Function.compile ({MYPATH}\drello\node_modules\mongoose\ lib\model.js:3178:32) at Mongoose.model ({MYPATH}\drello\node_modules\mongoose\li b\index.js:392:17) at Object. ({MYPATH}\drello\models\user.js:19:27 ) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.require (module.js:483:17) at require (internal/module.js:20:19) at Object. ({MYPATH}\drello\app.js:12:25) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3

NOTE: I will not get the error if I tried to add the custom method in the main script ( app.js ) but I will get it everywhere else

vkarpov15 commented 7 years ago

Yeah I'm going to close this for now because this issue's pretty stale and OP was never really clear about the issue.

jo-russ commented 7 years ago

I can provide more details if you'd like. Bit late, but I just yesteday stumbled upon the same problem.

When you add any new methods to Object object kareem somewhere inside fails on clone operation because it iterates there (for (var x in...) over object properties disregarding whether it is his own property or not (checkable via hasOwnProperty). so at some point it tries to clone function and dies.

vkarpov15 commented 7 years ago

Yes please, provide more details, preferably a script that lets me repro the issue. Prose descriptions always fail to properly convey code issues.

jo-russ commented 7 years ago

The simplest(-ish :D) I can think of.

var Kareem = require('kareem');
var hooks = new Kareem();
Object.prototype.dLikeABanana = function() { console.log("BANNNANAAAAA!!!"); }

var clone = hooks.clone();

var I = {}; 
I.dLikeABanana();

In my case it fails when used by mongoose: lib/ model.js in Model.compile()

vkarpov15 commented 7 years ago

Fixed, thanks for the repro