简体   繁体   English

如何通过mongoose,Node.js中的var设置密钥?

[英]how to set key by var in mongoose,Node.js?

I don't set variable the key for 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's 'this_key' in 但是this_key不是'test',而是'this_key'

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

I need get some value ext POST[] to set variable this_key, 我需要获得一些值ext POST []来设置变量this_key,

how to set key by variable in mongoose,Node.js? 如何在mongoose,Node.js中按变量设置键?

The syntax for string literals in object field names is biting you here. 对象字段名称中字符串文字的语法在这里咬你。 To get around it, make an intermediate object and construct 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){});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM