How to set var key in mongoose, Node.js?

I do not set the variable key to update, my code ...

mongoose.model('members', Schema).update({ id: '0' }, {$push: {'this_key': 'value'}} , [], function (err, data){}); 

if i use

 var this_key = 'test'; 

but this_key is not 'test' it 'this_key' in

  mongoose.model('members', Schema).update({ id: '0' }, {$push: {this_key: 'value'}} , [], function (err, data){}); 

I need to get some ext POST [] value to set this_key variable,

How to set a variable key in mongoose, Node.js?

+6
source share
1 answer

The syntax of string literals in object field names bites you here. To get around this, make an intermediate object and build it without using literals:

 var this_key = 'test'; var push = {}; push[this_key] = 'value'; // here, it will use the variable mongoose.model('members', Schema).update( { id: '0' }, {$push: push} , [], function (err, data){}); 
+13
source

Source: https://habr.com/ru/post/918909/


All Articles