1

I have a save() in a post route, that doesn't actually save anything. Can anybody tell me what I'm doing wrong ?

router.post('/addSchemaField/:id',(req,res,next)=>{

mongooseDynamic.addSchemaField(Profile, req.body.table+"."+req.body.name, {type : [String], default : ""});


Profile.findById(req.params.id, function (err, post) {

  if (err) return next(err);
  
  post[req.body.table][req.body.name][4] = req.body.typ;
  
  console.log(post[req.body.table][req.body.name]);
  
  post.markModified('post[req.body.table][req.body.name][4]');
  
  post.save(function(err, upPost){
    res.send(upPost);
  });
});  
res.end();
});
Irrelefant
  • 125
  • 1
  • 1
  • 7
  • where mongoose operations which they must save – Vadim Hulevich Jan 04 '19 at 12:01
  • Perhaps you meant the actual subdocument dynamic path as ``post.markModified(`${post[req.body.table]}.${[req.body.name]}`);``? – chridam Jan 04 '19 at 12:10
  • @chridam Thank you! that worked, though just one time... any other ideas ? – Irrelefant Jan 04 '19 at 12:25
  • Try ``post.markModified(`${req.body.table}.${req.body.name}`);`` – chridam Jan 04 '19 at 12:58
  • @chridam Still no luck, but I noticed that if I try to console.log "upPost" it is undefined in the save() function, which I also don't understand – Irrelefant Jan 04 '19 at 13:04
  • You could try to follow this [answer](https://stackoverflow.com/a/33327652/122005) – chridam Jan 04 '19 at 13:10
  • 1
    @chridam Okay, so I got it working now by just updating the whole table (i.e deleting ${req.body.name}) ! thank you for your continued support, would not have been able to work this out otherwise! – Irrelefant Jan 05 '19 at 11:12

0 Answers0