0

i want to update nested mongo document with for loop here is my node.js code;

//loop starts
    var update = {
      "rate":mainRate,
      "classifierCategories."+e+".rate":temiz[i].slice(0,2)
    };

    classifier.update({"classifierShortName":arrFile[1]},update,function(err){
    console.log("updated - "+i+" - "+e);
    });
//loop end

Error accurs ;

Unexpected token +

How can i update classifierCategories array with for loop

0

1 Answer 1

2

Your problem is how you are trying to notate the object "keys". This isn't valid for key construction in JavaScript object as the key names are literal and all characters are considered part of the name string.

Notate like this instead:

var update = { "rate": minRate };
update["classifierCategories."+e+".rate"] = temiz[i].slice(0,2);

That allows you to dynamically assign the key name like you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.