javascript - What happen here? (Array and pointer) -
i typing code, when (after 3 hours searching) found (sorry, main language spanish , don't know how explain this):
i have function in file makes array add object while keeping number of elements want:
exports.addtoarraykeepingnum = function(ob, arr, count){ if (arr.length < count){ arr[arr.length] = ob; }else{ var auxarr = new array(); var olength = arr.length; (var = (arr.length - 1); arr.length >= count; a--){ arr.shift(); } arr[arr.length] = ob; } }
but now, mongoose extract document , try make this:
tools.addtoarraykeepingnum({ip: req.ip, datetime: date.now()}, doc.last_visits, 15);
and then, mongoose doesn't accept document because has been changed schema... if do:
var arr = doc.last_visits; tools.addtoarraykeepingnum({ip: req.ip, datetime: date.now()}, arr, 15); doc.last_visits = arr;
it works... why? mean... passing reference, i'm not? should works first example... think...
if can explain me, helpful!
edit:
here error printed in console:
thu, 27 nov 2014 06:38:48 gmt uncaughtexception object #<object> has no method ' save' typeerror: object #<object> has no method 'save' @ c:\users\myname\desktop\theproject\node_modules\mongoose\lib\docum ent.js:1299:13 @ array.foreach (native) @ model.pre.err.stack (c:\users\myname\desktop\theproject\node_modul es\mongoose\lib\document.js:1281:12) @ model._next (c:\users\myname\desktop\theproject\node_modules\mongo ose\node_modules\hooks\hooks.js:50:30) @ model.proto.(anonymous function) [as save] (c:\users\myname\desktop\y amireproject\node_modules\mongoose\node_modules\hooks\hooks.js:96:20) @ promise.<anonymous> (c:\users\myname\desktop\theproject\lib\misc\l oginutils.js:76:17) @ promise.<anonymous> (c:\users\myname\desktop\theproject\node_modul es\mongoose\node_modules\mpromise\lib\promise.js:177:8) @ promise.emit (events.js:95:17) @ promise.emit (c:\users\myname\desktop\theproject\node_modules\mong oose\node_modules\mpromise\lib\promise.js:84:38) @ promise.fulfill (c:\users\myname\desktop\theproject\node_modules\m ongoose\node_modules\mpromise\lib\promise.js:97:20)
"save" it's method mongoose. can see it's lose his... identity (?)
Comments
Post a Comment