我正在输入一些代码,当(搜索后3个小时)我发现了这个(对不起,我的主要语言是西班牙语,我不知道如何解释这个):
我在另一个文件中有一个函数,它使一个Array添加一个对象,同时保留我想要的元素数量:
exports.addToArrayKeepingNum = function(ob, arr, count){
if (arr.length < count){
arr[arr.length] = ob;
}else{
var auxArr = new Array();
var oLength = arr.length;
for (var a = (arr.length - 1); arr.length >= count; a--){
arr.shift();
}
arr[arr.length] = ob;
}
}
但是现在,我从猫鼬中提取了一份文件并试图做到这一点:
Tools.addToArrayKeepingNum({ip: req.ip, datetime: Date.now()}, doc.last_visits, 15);
然后,Mongoose不接受该文件,因为它已经改变了架构......但如果我这样做:
var arr = doc.last_visits;
Tools.addToArrayKeepingNum({ip: req.ip, datetime: Date.now()}, arr, 15);
doc.last_visits = arr;
它有效......为什么?我的意思是......我正在通过参考,我不是吗?所以它应该适用于第一个例子......我想......
如果你能解释一下,那会很有帮助!
编辑:
以下是控制台中打印的错误:
Thu, 27 Nov 2014 06:38:48 GMT uncaughtException Object #<Object> has no method '
save'
TypeError: Object #<Object> has no method 'save'
at C:\Users\MyName\Desktop\TheProject\node_modules\mongoose\lib\docum
ent.js:1299:13
at Array.forEach (native)
at model.pre.err.stack (C:\Users\MyName\Desktop\TheProject\node_modul
es\mongoose\lib\document.js:1281:12)
at model._next (C:\Users\MyName\Desktop\TheProject\node_modules\mongo
ose\node_modules\hooks\hooks.js:50:30)
at model.proto.(anonymous function) [as save] (C:\Users\MyName\Desktop\Y
amireProject\node_modules\mongoose\node_modules\hooks\hooks.js:96:20)
at Promise.<anonymous> (C:\Users\MyName\Desktop\TheProject\lib\misc\l
oginUtils.js:76:17)
at Promise.<anonymous> (C:\Users\MyName\Desktop\TheProject\node_modul
es\mongoose\node_modules\mpromise\lib\promise.js:177:8)
at Promise.emit (events.js:95:17)
at Promise.emit (C:\Users\MyName\Desktop\TheProject\node_modules\mong
oose\node_modules\mpromise\lib\promise.js:84:38)
at Promise.fulfill (C:\Users\MyName\Desktop\TheProject\node_modules\m
ongoose\node_modules\mpromise\lib\promise.js:97:20)
&#34;保存&#34;它是Mongoose的一种方法。你可以看到它失去了......身份(?)