mongodb - Update nested sub document using Node.js and mongoose -


i tried save details nested sub document tertiary.

module.exports = mongoose.model('todo', {     title : string,     image:string,     bgimage:string,     secondary:[secondary] });  var secondary = new mongoose.schema({     title : string,     image:string,     bgimage:string,     tertiary :[tertiary] });  var tertiary = new mongoose.schema({     title : string,      description:string,     image:string }); 

the code saving tertiary data below. have primary object id secondary object id.

todo.findbyid(fields.primaryid, function (err, secondary_todo) {     if (!err) {         console.log("---inside not errot----");         console.log(fields.secondaryrefid);          secondary_todo.secondary.findbyid(fields.secondaryrefid,                                           function (err, tertiary_todo) {             console.log("---in secondary data----");             console.log(tertiary_todo);             if (!err) {                 tertiary_todo.tertiary.push({                      _id:  mongoose.types.objectid(),                     title : fields.title,                     image : fields.file,                     description : fields.description,                 });             }             tertiary_todo.save();         });      } }); 

the error got in terminal

/......./.../..../lib/utils.js:419     throw err;           ^   typeerror: object [object object],[object object],[object object] has no method 'findbyid' @ promise.<anonymous> 

can give quick fix. tried , struck it.

you may want read this page in docs describing sub docs , special id() method used find sub document give id.
instead of calling findbyid on sub documents

secondary_todo.secondary.findbyid(fields.secondaryrefid, function (err, tertiary_todo) {  ... 

use id

var tertiary_todo = secondary_todo.secondary.id(fields.secondaryrefid); ... 

update: issue schema setup broken use child schemas before defined. reorder them

var tertiary = new mongoose.schema({     title : string,      description:string,     image:string });  var secondary = new mongoose.schema({     title : string,     image:string,     bgimage:string,     tertiary :[tertiary] });  module.exports = mongoose.model('todo', {     title : string,     image:string,     bgimage:string,     secondary:[secondary] }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -