【问题标题】:Getting the _id of the record in autoform on a method-update in meteor在流星中的方法更新上以自动形式获取记录的_id
【发布时间】:2015-06-15 15:50:02
【问题描述】:

我将autoform package 用于流星,但是当我尝试更新集合中的文档时,我似乎无法获取记录的_id 来更新它。

我正在使用带有 type=method-update 的 autoform,所以我可以在服务器端验证它。当我尝试下面的代码时,它失败了,因为 _id 未定义。

模板:

{{#autoForm collection="Lessons" doc=lesson id="updateLessonForm"  type="method-update" meteormethod="updateLesson"}}
        <fieldset>
            {{> afFieldInput name="categoryId" firstOption="(Select a Category)" options=categoryOptions}}
            {{> afQuickField name='title'}}
            {{> afQuickField name='summary' rows=2}}
            {{> afQuickField name='detail' rows=1}}
            {{> afQuickField name='extras' rows=1}}
            {{> afQuickField name='active'}}
        </fieldset>
        <button type="submit" class="btn btn-primary btn-block">Update Lesson</button>
    {{/autoForm}}

服务器端方法:

updateLesson: function (doc) {
    check(doc, Lessons.simpleSchema());
    Lessons.update({_id: this._id}, doc);
}

更新:

doc._id returns undefined

doc returns:
I20150409-23:15:22.671(-5)? { '$set': 
I20150409-23:15:22.672(-5)?    { categoryId: 1,
I20150409-23:15:22.672(-5)?      title: 'Lesson 1 update',
I20150409-23:15:22.672(-5)?      summary: 'Summary for lesson 2',
I20150409-23:15:22.672(-5)?      detail: '<p>dsffdsfd</p>',
I20150409-23:15:22.672(-5)?      extras: '<p>fdsf</p>',
I20150409-23:15:22.672(-5)?      active: false } }

【问题讨论】:

    标签: mongodb meteor meteor-autoform


    【解决方案1】:

    如果你打印doc,你应该得到文档,所以this._id应该改为doc._id

    注意:在更新之前尝试使用console.log 查看您获得的值。

    console.log(this._id)//should return undefined.
    console.log(doc._id)//should return the id
    console.log(doc)//should return the doc.
    

    更新

    为了得到_id,你应该调用第二个参数。

    updateLesson: function (doc,doc_id) {
        check(doc, Lessons.simpleSchema());
        Lessons.update({_id: this._id}, doc);
    }
    

    【讨论】:

    • doc._id 返回未定义
    • I20150409-23:15:22.671(-5)? { '$set': I20150409-23:15:22.672(-5)? { categoryId: 1, I20150409-23:15:22.672(-5)?标题:“第 1 课更新”,I20150409-23:15:22.672(-5)?摘要:“第 2 课总结”,I20150409-23:15:22.672(-5)?详细信息:'

      dsffdsfd

      ',I20150409-23:15:22.672(-5)?附加功能:'

      fdsf

      ',I20150409-23:15:22.672(-5)?活跃:假}}
    • console.log(doc) 什么返回?我也完全忘记了你应该像这样Lessons.update({_id: doc._id}, {$set:{field:doc.newValue}}); 进行更新,你用钩子试过了吗?和onSuccess 回调?
    • 我编辑了问题以显示来自文档的日志。我正在使用方法更新github.com/aldeed/meteor-autoform#method-update 我不相信在使用方法类型时我需要做一个回调钩子。也许我的理解是错误的。使用方法更新它说它传递了两个参数,一个是documentId。我不知道如何在服务器端方法中访问 documentId。
    • 知道了,它说方法应该有2个参数,所以方法应该是这样的updateLesson: function (doc,doc_id) { console.log(doc) console.log(doc._id) check(doc, Lessons.simpleSchema()); Lessons.update({_id: this._id}, doc); }在方法上使用2个参数,doc,docID
    猜你喜欢
    • 2014-07-15
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多