0

I want to update element by its index number. I have data on Mongodb node.js like this:

{"_id" : ObjectId("5b533c33327d12098277c6a4"),
  "name":["aa","bb","cc"],
  "age":["45","50","40"],
   "home":["dd","ee","ff"]}

I want to change the value of each element of name, age and home. I tried it like this, but it doesn't work.

router.put("/forms/data/edit/:index/:id", function(req,res){
  var i = req.params.index;
  Datastored.findByIdAndUpdate(req.params.id,{$set:{
     name[i]:req.body.name,
     age[i]:req.body.age,
     home[i]:req.body.home}},
    function(err){
        if(err){
          console.log(err)
           res.redirect("back");
        }else{
          console.log("data edited");
          res.redirect("/seealldata");
     }
    });
});

I get the following error:

parsing error: Unexpected token [ " , on the line of code, name[i].req.body.name

Blue
  • 22,608
  • 7
  • 62
  • 92
wzwd
  • 169
  • 2
  • 10
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Jul 25 '18 at 17:24
  • i can't use the above code, because it says "parsing error: Unexpected token [ " , on the line of code, name[i].req.body.name, – wzwd Jul 25 '18 at 17:31

2 Answers2

1

After a bit of googling, I found this question/answer. Try the following:

router.put("/forms/data/edit/:index/:id", function(req,res){
  var i = req.params.index;
  Datastored.findByIdAndUpdate(req.params.id,{
    $set:{
     [`name.${i}`]: req.body.name,
     [`age.${i}`]: req.body.age,
     [`home.${i}`]: req.body.home
  }},
    function(err){
        if(err){
          console.log(err)
           res.redirect("back");
        }else{
          console.log("data edited");
          res.redirect("/seealldata");
     }
    });
});
Blue
  • 22,608
  • 7
  • 62
  • 92
  • I tried the above code but It doesn't work. it do console.log, data edited, no error displayed. But no changes on the data, or nothing updated or changes on my database. I don't know why – wzwd Jul 26 '18 at 02:32
0

You will need to construct the object like this first, {$set: {'field.2': value, ...}

router.put("/forms/data/edit/:index/:id", function(req,res){
  var i = req.params.index;
  var updateObj = { $set: {} };
  updateObj.$set["name."+i] = req.body.name;
  updateObj.$set["age."+i] = req.body.age;
  updateObj.$set["home."+i] = req.body.home;
  Datastored.findByIdAndUpdate(req.params.id, updateObj),
      function(err){
          if(err){
            console.log(err)
            res.redirect("back");
          }else{
            console.log("data edited");
            res.redirect("/seealldata");
          }
       });
 });
Helping hand
  • 2,800
  • 2
  • 19
  • 31
  • I tried the above code, but when I tried it the page keeps busy searching on edit page. I don't know the reason. nothing happen or my data is not updated – wzwd Jul 26 '18 at 02:38
  • @wzwd what error message did you get ? This will help me find the issue. – Helping hand Jul 26 '18 at 06:03
  • There is no error displayed. what happen is the page couldn't stop loading. The edit page (used to submit the post) couldn't stop loading, it keeps busy loading. – wzwd Jul 26 '18 at 06:33
  • Find what is printed in console. Make sure req.params.id value is correct and present in mongo collection document. – Helping hand Jul 26 '18 at 06:36
  • I hop you have understand what I wrote above. for example I put console.logI(i) below var i = req.params.index;, it prints the value, 1. No problem , but I think it could not pass the next line of code, because the page that sends the post (edit page) couldn't stop loading. I wait it a minute then it displays an html error on the edit page, says Error 502 - Bad Gateway – wzwd Jul 26 '18 at 06:51
  • This seems to be another issue related to server. Your doc is getting updated as per question – Helping hand Jul 26 '18 at 07:08
  • Thank you for helping me. I will try to find the point of problem a solution. I guess it is not server problem because when run other code like answered below it connects even though it doesn't work. – wzwd Jul 26 '18 at 07:30
  • i just do updateObj.$set["name."+i] = req.body.name; instead of updateObj["$set"]["name."+i], and it works. It was close, thanks anyway. – wzwd Jul 30 '18 at 18:51
  • Cool, I will update the answer accordingly, then. Must be some syntax that would not allow " "(quotes around $set, otherwise both things are same) – Helping hand Jul 30 '18 at 18:59