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

2
  • 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. Commented Jul 25, 2018 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, Commented Jul 25, 2018 at 17:31

2 Answers 2

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");
     }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

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
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");
          }
       });
 });

9 Comments

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 what error message did you get ? This will help me find the issue.
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.
Find what is printed in console. Make sure req.params.id value is correct and present in mongo collection document.
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 <title>Error 502 - Bad Gateway </title> …
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.