使用nodejs、mongoose和JSON对象数组进行拼接

时间:2023-02-06 21:27:09

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

我使用mongoose(指向mongolab)和nodejs,当我使用。findone并返回结果时,一个字段是JSON对象的数组,但是当我使用控制台时。记录我得到的数组

[ [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.

当我复制该数组的确切内容并手动创建它和控制台时。我看到了所有的JSON。这听起来不像是个问题,但是当我尝试将返回的数组拼接时,我遇到了问题。

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...

我尝试在for循环中遍历数组并记录每个元素,JSON显示良好。我有一个变通的办法,但不应该为了让它像它应该做的那样而做得更多……

    // 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.

我也尝试了数组。w3schools的splice示例确保splice正常工作。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log("Removed: " + fruits.splice(2,1,"Lemon"));
console.log(fruits);

// OUTPUTS
Removed: Apple
Banana,Orange,Lemon,Mango

3 个解决方案

#1


3  

From: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice

来自:https://developer.mozilla.org/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.

splice会更改原始数组。不要打印它返回的内容,打印您调用后给它的数组。

arr.splice(0, 1);
console.log(arr);

#2


1  

I was able to figure out the issue after much frustration. It turns out the array of objects coming 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.

工作之后。

#3


-1  

thanks user1123534...Your method has worked. instead of putting playlistSchema as

由于user1123534……你的工作方法。而不是把playlistSchema设为

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

并在playlistSchema as中使用上面的模式

var PlaylistSchema = new Schema({
  userid: {
    type: String,
    index: { unique: true }
  },
  playlist_name: String,
  songs: [SongSchema]
});

#1


3  

From: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice

来自:https://developer.mozilla.org/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.

splice会更改原始数组。不要打印它返回的内容,打印您调用后给它的数组。

arr.splice(0, 1);
console.log(arr);

#2


1  

I was able to figure out the issue after much frustration. It turns out the array of objects coming 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.

工作之后。

#3


-1  

thanks user1123534...Your method has worked. instead of putting playlistSchema as

由于user1123534……你的工作方法。而不是把playlistSchema设为

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

并在playlistSchema as中使用上面的模式

var PlaylistSchema = new Schema({
  userid: {
    type: String,
    index: { unique: true }
  },
  playlist_name: String,
  songs: [SongSchema]
});