node.js - Mongoose Pre Save hook on Parent Object not executing -


i new mongoose method updating mongo. using nmap map network , provide visibility on servers , ports open (as part of largeer strategy). portion of strategy alos pulls information chef , vsphere linked in gui. portions simple single level objects , working fine, nmap portion has parent/child object model. object model has server object , port object - both of have addedon , updatedon dates.

unfortunately mongoose hook pre save firing children objects (each of them) , not parent object - although parent object getting saved. i want parent object have addedon , updatedon dates. can't seem figure out. in advance

a disclaimer, code below snipped out of larger application written in folktale, ramda , point free form.

nodejs 5.1, mongoose 4.4.1

the port.js file

const mongoose = require('mongoose');  const  schema = mongoose.schema,     objectid = schema.objectid;  const portschema = new schema({     id               : objectid,     port             : string,     protocol         : string,     application      : string,     addedon          : { type: date, default: date.now, setdefaultsoninsert: true },     updatedon        : { type: date, default: date.now } });  portschema.pre('save', function(next){     this.update({},{         $set: {             updatedon: new date()         }     })     next(); });  module.exports.model = mongoose.model('port', portschema) module.exports.schema = portschema 

the server.js file

const mongoose = require('mongoose'); const portschema = require('./port').schema  const  schema = mongoose.schema,     objectid = schema.objectid;  const serverschema = new schema({     id               : objectid,     address          : string,     addresstype      : string,     ports            : [portschema],     addedon          : { type: date, default: date.now, setdefaultsoninsert: true },     updatedon        : { type: date, default: date.now } });  serverschema.pre('save', function(next){     this.update({},{         $set: {             updatedon: new date()         }     })     next(); });   module.exports.model = mongoose.model('server', serverschema) module.exports.schema = serverschema 

upsert code in application

// nmapupsert :: (criteria, {}) => task {ok :: int, ...} const nmapupsert = adapt.findoneandupdate(server)   // persist :: [{address :: string, ...}] => [task {ok :: int, ...}] const persistnmap = r.map((data) => {     return nmapupsert({ "address": data.address }, data) }) 

here model -> task upsert adapter (adapt.findoneandupdate)

module.exports.findoneandupdate = (originalmodel) => {     return (criteria, record) => {          return new task((reject, resolve) => {             const callback = (error, updatedmodel) => {                 if (error) {                     reject(error)                 }                 else {                     if(!updatedmodel) {                         resolve(null)                     }else {                         // looks required apply defaults schema                         updatedmodel.save((error) => {                             if (error) {                                 reject(error)                             }                             resolve(updatedmodel)                         })                     }                  }             }              originalmodel.findoneandupdate(criteria, record, {upsert: true}, callback)         })      } } 

admittedly last function bit cludgy - trying figure out before clean up.

if change hooks findoneandupdate better behavior. server objectgets addedon on insert (and updatedon well), subsequently updatedon gets updated. unforutnately entire ports object collection gets replaced - on update - , addedon , updatedon both updated @ point.

the port.js file

const mongoose = require('mongoose');  const  schema = mongoose.schema,     objectid = schema.objectid;  const portschema = new schema({     id               : objectid,     port             : string,     protocol         : string,     application      : string,     addedon          : { type: date, default: date.now, setdefaultsoninsert: true },     updatedon        : { type: date, default: date.now } });  portschema.pre('findoneandupdate', function(next){     this.update({},{         $set: {             updatedon: new date()         }     })     next(); });  module.exports.model = mongoose.model('port', portschema) module.exports.schema = portschema 

the server.js file

const mongoose = require('mongoose'); const portschema = require('./port').schema  const  schema = mongoose.schema,     objectid = schema.objectid;  const serverschema = new schema({     id               : objectid,     address          : string,     addresstype      : string,     ports            : [portschema],     addedon          : { type: date, default: date.now, setdefaultsoninsert: true },     updatedon        : { type: date, default: date.now } });  serverschema.pre('findoneandupdate', function(next){     this.update({},{         $set: {             updatedon: new date()         }     })     next(); });   module.exports.model = mongoose.model('server', serverschema) module.exports.schema = serverschema 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -