Mongodb 在meteor中的方法更新中获取autoform中记录的_id
我正在使用for meteor,但是当我尝试更新集合中的文档时,似乎无法获取记录的Mongodb 在meteor中的方法更新中获取autoform中记录的_id,mongodb,meteor,meteor-autoform,Mongodb,Meteor,Meteor Autoform,我正在使用for meteor,但是当我尝试更新集合中的文档时,似乎无法获取记录的\u id来更新它 我正在使用autoform和type=methodupdate,以便在服务器端验证它。当我尝试下面的代码时,它失败了,因为\u id未定义 模板: {{#autoForm collection="Lessons" doc=lesson id="updateLessonForm" type="method-update" meteormethod="updateLesson"}}
\u id
来更新它
我正在使用autoform和type=methodupdate
,以便在服务器端验证它。当我尝试下面的代码时,它失败了,因为\u 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}}
更新:
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.\u id返回未定义的
单据退回:
I20150409-23:15:22.671(-5)?{'$set':
I20150409-23:15:22.672(-5)?{类别ID:1,
I20150409-23:15:22.672(-5)?标题:“第1课更新”,
I20150409-23:15:22.672(-5)?小结:“第二课小结”,
I20150409-23:15:22.672(-5)?细节:“dsffdsfd”,
I20150409-23:15:22.672(-5)?附加条款:“fdsf”,
I20150409-23:15:22.672(-5)?活动:假}
如果您打印文档,您应该获得文档,因此此。\u id
应更改为文档。\u id
注意:在执行更新之前,请尝试使用控制台.log
查看您获得的值
console.log(this._id)//should return undefined.
console.log(doc._id)//should return the id
console.log(doc)//should return the doc.
更新
为了获得\u id
,您应该调用第二个参数
updateLesson: function (doc,doc_id) {
check(doc, Lessons.simpleSchema());
Lessons.update({_id: this._id}, doc);
}
单据id返回未定义20150409-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.672(-5)?详细信息:“dsffdsfd p>”,I20150409-23:15:22.672(-5)?附加内容:“fdp>”,I201504009-23:23:672(-5)和
console.log(doc)
返回什么?而且我完全忘了你应该像这样更新Lessons.update({u id:doc.{u id},{$set:{field:doc.newValue})代码>你用钩子试过了吗?还有onSuccess
callback?我编辑了这个问题以显示文档中的日志。我正在使用方法更新,我认为在使用方法类型时不需要执行回调挂钩。也许我的理解是错误的。方法update表示它传递两个参数,一个是documentId。我无法理解documentId在服务器端方法中是如何被访问的。明白了,它说这个方法应该有两个参数,所以这个方法应该是这样的updatelsson:function(doc,doc\u id){console.log(doc)console.log(doc.\u id)check(doc,Lessons.simpleSchema());Lessons.update({\u id:this.\u id},doc);}
在方法上使用两个参数,doc,docID
updateLesson: function (doc,doc_id) {
check(doc, Lessons.simpleSchema());
Lessons.update({_id: this._id}, doc);
}