admin管理员组文章数量:1410724
I am using mongoose (pointing to mongolab) with nodejs and when I do .findOne
and get a result back, one field is an array of JSON objects but when I console.log
the array I am getting
[ [object Object], [object Object], [object Object], [object Object], [object Object] ]
When I copy the exact contents of the array and manually create it and console.log
it I see all the JSON. This doesn't sound like a problem, but when I try to splice this array returned, I am running into issues.
console.log(arr.splice(0, 1));
outputs
[ [object Object], [object Object], [object Object], [object Object] ]
So it is returning all of the objects that remain and not the deleted one. When I do the splice on the array I created by copying exactly what is in the array that is causing problems it works fine (the element returned is the one I deleted).
I tried going through the array in a for loop and logged each element and the JSON appeared fine. I have a workaround but shouldn't have to do this much more for it to work like it should...
// outputs [ [object Object], [object Object]... ]
console.log(s);
for (var i = 0; i < s.length; i++) {
if (i == start) {
el = s[i];
break;
}
}
s.splice(start, 1);
s.splice(end, 0, el);
for (var i = 0; i < s.length; i++) {
// outputs the JSON for each element
console.log(s[i]);
}
I also tried the array.splice example on w3schools to make sure splice worked in general and it worked fine.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log("Removed: " + fruits.splice(2,1,"Lemon"));
console.log(fruits);
// OUTPUTS
Removed: Apple
Banana,Orange,Lemon,Mango
I am using mongoose (pointing to mongolab) with nodejs and when I do .findOne
and get a result back, one field is an array of JSON objects but when I console.log
the array I am getting
[ [object Object], [object Object], [object Object], [object Object], [object Object] ]
When I copy the exact contents of the array and manually create it and console.log
it I see all the JSON. This doesn't sound like a problem, but when I try to splice this array returned, I am running into issues.
console.log(arr.splice(0, 1));
outputs
[ [object Object], [object Object], [object Object], [object Object] ]
So it is returning all of the objects that remain and not the deleted one. When I do the splice on the array I created by copying exactly what is in the array that is causing problems it works fine (the element returned is the one I deleted).
I tried going through the array in a for loop and logged each element and the JSON appeared fine. I have a workaround but shouldn't have to do this much more for it to work like it should...
// outputs [ [object Object], [object Object]... ]
console.log(s);
for (var i = 0; i < s.length; i++) {
if (i == start) {
el = s[i];
break;
}
}
s.splice(start, 1);
s.splice(end, 0, el);
for (var i = 0; i < s.length; i++) {
// outputs the JSON for each element
console.log(s[i]);
}
I also tried the array.splice example on w3schools to make sure splice worked in general and it worked fine.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log("Removed: " + fruits.splice(2,1,"Lemon"));
console.log(fruits);
// OUTPUTS
Removed: Apple
Banana,Orange,Lemon,Mango
Share
Improve this question
asked Jan 5, 2012 at 20:07
mattsimonismattsimonis
411 silver badge4 bronze badges
3 Answers
Reset to default 3From: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Array/splice
Returns
An array containing the removed elements. If only one element is removed, an array of one element is returned
splice
changes the original array. Don't print what it returns, print the array that you gave it after you called it.
arr.splice(0, 1);
console.log(arr);
I was able to figure out the issue after much frustration. It turns out the array of objects ing back was not properly set up in my schema. I needed to define an object that would be in the array, and then for that property in the schema have something like this.
var SongSchema = new Schema({
title: String,
url: String,
source: String,
duration: String
});
var PlaylistSchema = new Schema({
userid: {
type: String,
index: { unique: true }
},
playlist_name: String,
songs: [SongSchema]
});
Worked after that.
thanks user1123534...Your method has worked. instead of putting playlistSchema as
var PlaylistSchema = new Schema({
userid: {
type: String,
index: { unique: true }
},
playlist_name: String,
songs: [
title: String,
url: String,
source: String,
duration: String
]
});
create a new schema for embedded properties in array as in
var SongSchema = new Schema({
title: String,
url: String,
source: String,
duration: String
});
and use the above schema in playlistSchema as
var PlaylistSchema = new Schema({
userid: {
type: String,
index: { unique: true }
},
playlist_name: String,
songs: [SongSchema]
});
本文标签: javascriptSplice with nodejsmongoose and array of JSON objectsStack Overflow
版权声明:本文标题:javascript - Splice with nodejs, mongoose and array of JSON objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775850a2624617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论