将对象迭代为通过postman工具发送并保存在db中的对象数组

时间:2017-12-14 07:08:56

标签: json node.js mongoose postman

这是我通过邮递员工具发送的价值

{
    "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"
    }]
}

我在控制层中获得了值,并且我试图使用mongoose将其保存在我的mongodb中,我的模型代码是

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;

我正在迭代的服务层代码

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);
        }
    );

}

我的服务层有一个console.log(),但它只给我空值,我认为我的模型代码或我的服务层迭代有问题! enter image description here

0 个答案:

没有答案