正在编辑的对象中的 mongodb 查询字段 [英] mongodb query field in object that is being edited

查看:52
本文介绍了正在编辑的对象中的 mongodb 查询字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查询对象中的字段?我的 html 检索名为明信片"的数组中的所有对象

How can I query a field in an object? My html retrieves all the objects in array called 'postcards'

Meteor.user.profile.postcards [
    {
    _id: 84fh83f,
    field_one: "a name",
    field_two: " winter whether",
    field_three: " lost more writing"
    },
    {
    _id: 6jsf58s,
    field_one: "another  name",
    field_two: " topical issues ",
    field_three: " lost more writing"
    }
   ]

注意:我使用了 random.Id() 以便可以唯一标识数组中的每个对象.

Note: I used random.Id() so each object in the array can be uniquely identified.

当用户专注于输入字段时,将会话值设置为 this._id 将检索此唯一 ID,但是,我想查询焦点中的实际字段.这些字段中的值通过使用 html 中的空格键语法投影到文本输入区域内.

Setting a session value to this._id when the user is focused on the input field will retrieve this unique id, however, I would like to query the actual field in focus. The value in these fields are projected within the text input area by using the spacebars syntax within the html.

我可以以某种方式将 value 属性的大括号内的名称分配给变量吗?然后查询?

Can I somehow assign the name within the curly braces of the value attribute to a variable? Then query?

有没有一种全新的方法来实现这一目标?

Is there a whole new way to achieve this?

我想更新此对象中的特定字段,而不是更新整个对象.

HTML:

{{#with currentUser.profile}}
 {{#each postcards}}
   <input id="one" value="{{field_one}}" type="text">
   <input id="two" value="{{field_two}}" type="text">
   <input id="three" value="{{field_three}}" type="text">
 {{/each}}
{{/with}}

client.js

在事件中,我想更新该字段以专注于 keyup.

Within events, I would like to update the field on focus upon keyup.

Templates.myTemplate.events({
 'keyup input[type=text]': _.throttle(function(event) {
  Meteor.users.update(this._id, {$set: {**fieldbeingedited**: event.target.value}});
  }, 500);
  });

推荐答案

您想要的是名为计算属性名称"的 ES6 功能.

What you want to have is an ES6 capability named 'Computed property names'.

这看起来像:

var x = 'hello',
    obj = {
      [x]: 'world'
    };
console.log(obj); // Object {hello: "world"}

您有两个选择:- 您使用meteor Harmonic 包将您的es6 转换为es5 (https://github.com/mquandalle/meteor-和谐)- 你首先构建你的对象

You have two options : - you use the meteor harmony package to transpile your es6 to es5 (https://github.com/mquandalle/meteor-harmony) - you build your object first

首先建立你的对象:

var obj = {};
obj[ this.targetField ] = event.target.value
Meteor.users.update(this._id, {$set: obj});

这篇关于正在编辑的对象中的 mongodb 查询字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆