【问题标题】:How to update document of particular user id如何更新特定用户 ID 的文档
【发布时间】:2017-09-13 07:23:35
【问题描述】:

基本上我想做的是,只需更新用户详细信息。所以当 我输入用户 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 文件。任何人都可以帮助我解决这个问题,因为我是流星和 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>

【问题讨论】:

标签: meteor


【解决方案1】:

欢迎来到流星 :)

更新文档时必须使用$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 时几乎没有区别。

【讨论】:

  • 我已经添加了我的html代码也请告诉我这是正确的写作方式。
  • @Nisha:请正确格式化您的代码。很难阅读,是的!代码是正确的。你可以这样使用它。顺便说一句,如果我的回答帮助您实现了解决方案,请接受答案。验证您的 html 是另一个问题:)
  • errorClass {isClientSafe: true, error: 403, reason: "Not allowed. Untrusted code may only update documents by ID.", details: undefined, message: "Not allowed. Untrusted code may only update按 ID 列出的文档。[403]",...} 我遇到了错误。
  • 谢谢@Ankur Soni。你建议的链接有助于解决上述问题。它工作正常。现在我想做的是,我想在输入用户 ID 时显示用户详细信息在 url. 有什么办法吗?
猜你喜欢
  • 2022-11-19
  • 2019-04-10
  • 1970-01-01
  • 2013-12-05
  • 2014-09-25
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多