pertama saya telah menyimpan Bangunan dan bagian :
async.every(uniqueSection,function (uS,callback) {
if (uS != undefined) {
console.log(uS);
Buildings.findOne({buildingname:uS.building}, function (err,doc) {
if (doc) {
// console.log(doc);
Sections.findOne({buildings:doc._id,section_type:uS.section},function (er,sd) {
if (sd) {
// then dont save
console.log(sd);
}
if (er) {
console.log(er);
}
if (!sd) {
// then save
var sect = new Sections();
sect.buildings = doc._id;
sect.section_type = uS.section;
sect.status = 'active';
sect.save(function (err,st) {
if(err) console.log(err);
console.log(st);
})
}
})
}
if (!doc) {
if (uS.building != undefined) {
var building = new Buildings();
building.buildingname = uS.building;
building.status = "active";
building.save(function (er,dc) {
if (dc) {
// console.log(dc);
Sections.findOne({buildings:dc._id,section_type:uS.section},function (er,sd) {
if (sd) {
// then dont save
console.log(sd);
}
if (er) {
console.log(er);
}
if (!sd) {
// then save
var sect = new Sections();
sect.buildings = dc._id;
sect.section_type = uS.section;
sect.status = 'active';
sect.save(function (err,st) {
if(err) console.log(err);
console.log(st);
})
}
})
}
if (er) {
console.log(er);
}
})
}
}
if (err) {
console.log(err);
}
})
}
})
maka saya telah menyimpan Unit oleh :
async.waterfall([
function(callback) {
Buildings.findOne({buildingname:d.building}, function (err,doc) {
if (doc) {
buildingId = doc._id;
callback(null, doc);
}
if (err) {
console.log(err);
}
})
},
function(doc,callback) {
Sections.findOne({buildings: buildingId,section_type:d.section},function (er,sd) {
if (sd) {
sectionId = sd._id;
callback(null,doc,sd);
}
if (er) {
console.log(er);
}
})
},
function (bld,st,callback) {
var s = d.shares.replace(",","");
var unit = {
unit_type: d.unit_type,
unit_num: d.residenceone_unit_id,
unit_ac_num: d.official_unit_id,
buildings: bld._id,
sections: st._id,
shares: s
}
Units.findOne(unit,function (err,unt) {
if (err) {
console.log(err);
}
if(unt) {
console.log(unt)
}
if (!unt) {
var units = new Units();
units.unit_type = d.unit_type;
units.unit_num = d.residenceone_unit_id;
units.unit_ac_num = d.official_unit_id;
units.buildings = bld._id;
units.sections = st._id;
units.shares = s;
units.save(function (er,doc) {
if (er) console.log(er);
console.log(doc);
})
}
})
}
], function(err) {
console.log(err)
});