【问题标题】:How to get Meteor collection field value using variable as field key如何使用变量作为字段键获取 Meteor 集合字段值
【发布时间】:2015-04-10 18:49:32
【问题描述】:

我将给出一个代码示例来描述我的问题:

var id = Collection.insert({
    name: 'Charles Darwin',
    likes: 1
});

var someVariable = 'name';

Collection.findOne(id).name // This returns 'Charles Darwin', but how do I use someVariable to get the same result?
Collection.findOne(id).someVariable // This will certainly not work, but what is the right way to do it?

【问题讨论】:

  • 不要忘记点击投票箭头附近的符号来接受答案。恭喜您提出第一个问题! (哦,顺便说一句,也试着从代码中提出问题,以使其非常清楚)。

标签: javascript node.js mongodb meteor


【解决方案1】:

尝试使用 bracket notation 访问对象属性:

Collection.findOne(id)[someVariable];

【讨论】:

猜你喜欢
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 2021-11-08
  • 2018-05-06
  • 2022-01-17
  • 2014-01-29
  • 1970-01-01
相关资源
最近更新 更多