Mongoose模式与嵌套的对象数组无法匹配

时间:2016-03-24 09:23:52

标签: javascript mongoose

经过几个小时的战斗,我想我真的需要帮助。下面是我的插入代码:

================
APP.JS
================

app.post('/scrape', function (req, res) {

    var newMainModel = new mainModel(); 
    newMainModel.save(req.body.data, function(err,data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
        res.end()
    });

});

====================================
THIS IS MY MODELS/MAIN.JS
=================================
var mongoose = require('mongoose');
var LyricSchema = mongoose.Schema({
    artists:
   { id: String,
     name: String,
     images: String
},
  albums:[{ album: { id:String, name:String, image:String }, tracks: {name: [String]} }]
});



var Lyric = module.exports = mongoose.model('lyric', LyricSchema);

module.exports.save = function(json, callback){
    Lyric.create(json, callback);
}

插入后,回调函数返回给我,这不是我想要的,相册数组不应为空。

{ artists: {}, albums: [], _id: 56f3ae5c3c9407f81afcb130, __v: 0 }

以下是我发送到文档中的内容:

{
  "artists": {
    "id": "4dpARuHxo51G3z768sgnrY",
    "name": "Adele",
    "images": "https://i.scdn.co/image/f8737f6fda048b45efe91f81c2bda2b601ae689c"
  },
  "albums": [
    {
      "album": {
        "id": "1cCEfOTFdaZo0POsxE5SkM",
        "name": "21",
        "image": "https://i.scdn.co/image/4e370e394e5b5ae0c4ab07fc0dcff31239c3da9c"
      },
      "tracks": {
        "name": [
          "Rolling in the Deep",
          "Rumour Has It"
        ]
      }
    },
    {
      "album": {
        "id": "6Pb3K1oPXdhsqFXtzKe3Z1",
        "name": "19",
        "image": "https://i.scdn.co/image/1ee6e01e369cf01b9f2eeb1437de8adf746798fd"
      },
      "tracks": {
        "name": [
          "Daydreamer",
          "Best For Last",
          "Melt My Heart to Stone",
          "My Same",
          "That's It, I Quit, I'm Moving On",
          "Chasing Pavements"
        ]
      }
    }
  ]
}

0 个答案:

没有答案