如何在mongoose,Node.js中通过var设置密钥? [英] how to set key by var in mongoose,Node.js?
本文介绍了如何在mongoose,Node.js中通过var设置密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有将变量设置为更新密钥,我的代码...
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){});
如果我使用
var this_key = 'test';
但是this_key不是'test',它是'this_key'
but this_key is not 'test' it's 'this_key' in
mongoose.model('members', Schema).update({ id: '0' }, {$push: {this_key: 'value'}} , [], function (err, data){});
我需要获取一些ext POST []值来设置变量this_key,
I need get some value ext POST[] to set variable this_key,
如何在mongoose,Node.js中按变量设置密钥?
how to set key by variable in 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){});
这篇关于如何在mongoose,Node.js中通过var设置密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文