mongodb - 在 meteor 中的方法更新中获取 autoform 中记录的 _id

标签 mongodb meteor meteor-autoform

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

我将自动表单与 type=method-update 结合使用,因此我可以在服务器端对其进行验证。当我尝试下面的代码时,它失败了,因为 _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 } }

最佳答案

如果打印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);
}

关于mongodb - 在 meteor 中的方法更新中获取 autoform 中记录的 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552744/

相关文章:

javascript - Mongoose 填充不填充

c语句相当于mongo查询db.users.find({age {$gt : 3}}, {})

javascript - Mongo/Meteor 游标更新事件

meteor - 使用 Meteor 和 Meteorhacks 进行服务器端渲染 :ssr and iron-router

meteor - 如何使用Twilio从用户的手机号码发送文本?

javascript - 对象和文件上传字段未显示

node.js - 使用socket.io、nodejs和mongodb处理聊天系统

javascript - async await node.js 跳过第二个 await 语句保存 mongodb

meteor - MDG ValidatedMethod 与 Aldeed Autoform : "_id is not allowed by the schema" error

javascript - Meteor 自动表单提交和模态驳回