iterating objects as array of objects sent through

2019-04-16 06:12发布

问题:

this is the value i'm sending through postman tool

{
    "name" :[
        {
        "first_name" : "antony",
        "second_name" : "grijan"
    },{
        "first_name" : "suresh",
        "second_name" : "muthu"
    }],
    "allergy" : [
        {
        "condition" : "headache"
    },
    {
        "condition" : "toothache"
    }],
    "communication" : [
        {
        "address" : "no 21 big street",
        "phone" : "84"
    },
    {
        "address" : "no 43 small street",
        "phone" :"87"
    }]
}

I got the value in my control layer and i'm trying to save it in my mongodb using mongoose, my model code is

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var patientSchema = new Schema({
    name: {
        first_name : { type : String, default : ''},
        second_name : { type : String, default : ''} 
    },
    allergy : {
        condition : {type : String, default : ''}
    },
    communication : {
        address : {type : String, default : ''},
        phone : {type : String, default : ''}

    }
});

var patients = mongoose.model('Patients',patientSchema);
module.exports = patients;

My service layer code where i'm iterating is

var addDao = require('../dao/dao');
var async = require('async');
module.exports.addPatient = function(detail,callback) {
    async.mapValues(detail,function(value,key,callback){
        addDao.addPatient(value,function(data){
            console.log(data);
            console.log("calling");
            callback(null, data);
        })
        }, function(err, result) {
            // result is now a map of results for each key
            console.log("inside func",result);
           callback(result);
        }
    );

}

I have a console.log() in my service layer but it gives me only empty values, i think there is something wrong either with my model code or my iteration in my service layer!