【问题标题】:How to modify Database from UI in Meteor App如何在 Meteor App 中从 UI 修改数据库
【发布时间】:2015-03-20 15:58:53
【问题描述】:

我是流星新手,想从用户界面修改数据库文档,

我知道我们必须使用 update function 但无法使用它,因为我想通过点击命令从 UI 编辑集合,请建议如何去做。

参数

选择器 Mongo 选择器、对象 ID 或字符串

Specifies which documents to modify

修饰符 Mongo修饰符

Specifies how to modify the documents

回调函数

**Optional**. If present, called with an error object as the first argument and, if no error, the number of affected documents as the second.

请建议如何使用修饰符。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    让我们举个例子,使用事件处理程序。

    Template.example.events({
     'click #updateThis':function(e,t){
       var newValue = t.$('.newValue').val(); // taking value from random input
       Collection.update({_id:this._id},{$set:{value:newValue}},function(error,result){
          if(error){
            console.log(error.reason)
           }else{
            console.log("Nice update")
           }
        })
      }
    })
    

    首先是选择器,就像它说的应该是要修改的文档的ID

    本例中的修饰符是$set 更多about field update operators here

    回调是让它异步,我喜欢使用errorresult的两个参数

    【讨论】:

    • 说我的桌子有两个变量:书名和价格,怎么办?
    • 改成这个{book:bookName,price:bookPrice}
    • Hey Ethaan,它不工作 :(。我必须使这些字段可编辑?
    • @garima 给我 10 分钟,我将在流星垫上做一个最小的例子
    • 这里是Meteorpad @garima
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多