node.js api with array's

时间:2015-07-13 23:12:56

标签: javascript arrays node.js mongoose

我对node.js和javascript很新。我已经建成并且api,突然之间数据模型发生了变化,现在我有点迷失了。

这是我的代码:

var mongoose = require('mongoose');


//create schema
var MedicSchema = new mongoose.Schema({
  nombre:{
      type: String,
      required: true
  },
  especialidad:{
    type: String,
    required: true
  },
  ranking:{
    type: Number,
  },

  direccion: [{
        direccion: {type: String, required: true},
        telefono: {type: String},
        horario:{type: String}
        }],

    foto:{
    type: String,
}
});

MedicSchema.find({})
.populate('direccion')
.exec(function (err, medic) {
    console.log(medic.direccion); // This should have your information now
});

//export model
module.exports = MedicSchema;

我能够将数据发送到direccion数组......但是当我调用api时;我明白了:

{
    "_id": "557839b36bcdd00e09173e36",
    "nombre": "Sra. Hong",
    "especialidad": "general",
    "__v": 0,
    "direccion": [
        {
            "_id": "55a44392b2e774572b957a8e"
        },
        {
            "_id": "55a44392b2e774572b957a8d"
        }
    ]
}

我找不到调用数组细节的方法。

编辑:我将其发布在bitbucket https://bitbucket.org/weput/api-server

1 个答案:

答案 0 :(得分:0)

你需要.populate你的“direccion”数组。例如:

MedicSchema.find({})
.populate('direccion')
.exec(function (err, medic) {
    console.log(medic.direccion); // This should have your information now
});