mongodb query field in object that is being edited

允我心安 提交于 2019-12-11 23:12:57

问题


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"
    }
   ]

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

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.

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?

I want to update that specific field in this object instead updating the entire object.

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

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);
  });

回答1:


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

This is what is looks like :

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

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

To build you object first :

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


来源:https://stackoverflow.com/questions/27485228/mongodb-query-field-in-object-that-is-being-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!