0

after searching and trying a lot of different things, I find myself in front of this problem : I want to post a content and save the id to an object that belongs in another schema, I'm using mongoose.

Project.findByid is finding the good project and if I log project after modification, the item is as I want it but the save part just doesn't work.

My question is : Is it possible to do a PUT action inside of a POST request, I tried to remove the first .save and it's not working either.

app.post('/pages', (req, res) => {
  var db = req.db;
  var name = req.body.name;
  var parent = req.body.parent;
  var myId = mongoose.Types.ObjectId();
  var new_content = new Content({
    name: name,
    _id: myId,
  })
  new_content.save(function (error, item) {
    if (error) {console.log(error)}
  })
  Project.findById(parent, 'content', function (error, project) {
    if (error) { console.error(error); }
    project.content[name] = myId;
    project.save(function (error) {
      if (error) {
        console.log(error)
      }
      res.send({
        success: true
      })
    })
  })
})
Sunkhern
  • 248
  • 1
  • 5
  • 19
  • I'm not sure what you want to do but you CAN'T do PUT within POST but you DEFINITELY CAN do multiple `save()` in ONE POST action. – Chau Tran Nov 28 '17 at 19:26

1 Answers1

0

Simply use

app.all(path, callback [, callback ...])

Instead of

app.post

More info here ExpressJS Docs

developerbh
  • 180
  • 10
  • I made your modification but it didn't make a difference ( but the ExpressJs doc was very interesting). I did a few tests, if I try to edit project.domain which is a String and already exist, the save function works. It seems it is more a problem linked to the object `content` that is not being updated. – Sunkhern Nov 30 '17 at 13:11
  • Are u sure that values u r posting (sending) are being sent? – developerbh Nov 30 '17 at 13:13
  • Yes, I can track them with console.logs until the end of the function and at every stage of it, after making even more tests here is where I am : `project.content = {foo : myId};` works but erase prev values. `project.content = {foo: myId, bar: myId};` testing multi, works. `tmp = {}; tmp[name] = myId; project.content = tmp;` this works also but same issue, it erase the previous `project.content` values. `tmp = project.content; tmp[name] = myId; project.content = tmp;` This doesn't work and `project.content` doesn't change val. – Sunkhern Nov 30 '17 at 13:44
  • I really don't understand what u mean, but if u r posting multiple values, then u must be doing ut using an array thus u need to make a loop and put the save method inside the loop – developerbh Nov 30 '17 at 13:49
  • I have a value `content` in my model which holds an `object`, I want to be able to add a new item to this object when I do my request but the best I can do for now is totally replace my content object with something else which is not what I want to do. – Sunkhern Nov 30 '17 at 14:49
  • Here this might help u, adding a new item to an object can be done using push method https://www.w3schools.com/jsref/jsref_push.asp – developerbh Nov 30 '17 at 14:52
  • U can push item to array or object it doesn’t matter, [see here ](https://stackoverflow.com/a/7261466) - if u r new in JS world, i suggest u learn the basics first – developerbh Dec 01 '17 at 08:41