简体   繁体   中英

node.js api with array's

I'm pretty new to node.js and javascript. I'm builded and api and out of the sudden the data model changed and now i'm kinda lost.

This is my code:

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;

I'm able to send data to the direccion array... but when i call the api; i get this:

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

i can't find a way to call the details of the array.

edit: i posted it on bitbucket https://bitbucket.org/weput/api-server

You need to .populate your "direccion" array. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM