如何更新特定用户ID的文档

时间:2017-09-13 07:23:36

标签: meteor

基本上我想做的是,只需更新用户详细信息 我输入用户ID,显示用户的所有其他详细信息 文本框。然后我可以编辑任何东西。

以下是我的js代码:

   Template.body.onCreated(function() {
        this.currentTextBox = new ReactiveVar();
   });

   Template.body.events({
       "keyup .new-task1"(event, instance) {
            instance.currentTextBox.set(event.target.value);
       }
   });

   Template.body.helpers({
       dbs() {
           const instance = Template.instance();
           return Dbs.find({'user_id':instance.currentTextBox.get()});
       }
   });

   Template.temp.events({
   "submit .sub"(event) {
       const target= event.target;
       const name1 =target.name.value;
       const add1 =add.value;   
       const r1 = target.r.value;
       const const1 = target.const.value;
       console.log(doc_id1);
       Dbs.update({"user_id" : "pid"},{"name" : "name1","r" : "r1","const" 
       : "const"}); 
   }
   });

现在问题是我无法更新已编辑的字段,尽管已编辑字段的值从html发送到js文件。有人帮助我解决此问题,因为我是meteor和mongo db的新手。

Html代码:

  <head>
  <title>eg1</title>

  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" 
  rel="stylesheet">
  </head>

 <body>
 <nav class="#e57373 red lighten-2">
 <div class="container">
 <div class="nav-wrapper">
 <a href="#" class="brand-logo">hello</a>
   <ul id="nav-mobile" class="right hide-on-med-and-down">
   </ul>
   </div>
   </div>
   </nav>

  <div class="row">
  <div class="col s2">

    <form class="new-task">
    <input type="text" name="pid" placeholder="patient_id" class="new-
    task1"/>
    </form>
    </div>
    <div class="col s4">
    <ul class="collection">
    {{#each dbs}}
    {{>temp}}
    {{/each}}
    </ul><br><br>
    </div>
    </div>
    </body>

    <template name="temp">
    <li class="collection-item">
    <form class="sub">
    <input type="text" name="user_id" value=" {{user_id}}"/>          
    <input type="text" name="addd" value=" {{add}}"/>
    <input type="text" name="r" value=" {{r}}"/>
    <input type="text" name="const" value=" {{const}}"/>

    <center><input type="submit" value="Submit" class="waves-effect 
    #607d8b grey btn"></center>
    </form>
    </li>
    </template>

1 个答案:

答案 0 :(得分:0)

欢迎来到流星:)

更新文档时必须使用$set属性

使用如下,让我知道。

Dbs.update({"user_id" : "pid"},{$set:{"name" : "name1","r" : "r1","const" : "const"}});

有关Collection.update Click Here的更多信息。

还可以通过Mongo Update Query example 了解如何在直接使用mongo shell时更新文档。

请记住,当您直接使用 Mongo Shell命令 Meteor Collections 时,几乎没有差异。