【发布时间】:2018-03-09 07:32:26
【问题描述】:
我正在使用 Meteor 和 MongoDB 开发一个应用程序,我正在尝试使用嵌套回调来访问新插入的文档,如下所示。但是,即使我在插入语句的成功回调中,我仍然在数据库中没有匹配的文档的情况下收到错误消息。我不确定为什么 Mongo 找不到我刚刚插入的文档。我知道这些方法是异步的,但我认为一旦 find 函数返回,回调将允许我访问新插入的文档。我试图用它自己的回调将 find 放在插入语句之外,并得到了同样的错误。
我也附上了错误信息。非常感谢您对此事的任何帮助!
insertEntryForm.call(entryFormObj, (error, result) => {
if (error) {
console.log(error);
toastr['error'](error.reason);
}
else {
toastr['success']("Entry form created!");
EntryForms.find({_id: result}, function(err, res) {
console.log(res);
});
}
}
);
【问题讨论】:
-
你能发一下你的
entryFormObj吗? -
EntryForms.schema = new SimpleSchema({ _id: { type: String, regEx: SimpleSchema.RegEx.Id }, event: { type: String }, email: { type: String, optional: true }, name: { type: String }, userId: { type: String, optional: true }, entries: { type: [Object], defaultValue: [] }, }); -
export const insertEntryForm = new ValidatedMethod({ name: 'entryForms.insert', validate: EntryForms.simpleSchema().pick(['event', 'email', 'name', 'userId']).validator({clean: true, filter: false}), run({event, email, name, userId}) { if(!this.userId) { throw new Meteor.Error("entryForms.insert", "Not authorized to create new entry form"); } const entryForm = { event, email, name, userId, }; const entryFormId = EntryForms.insert(entryForm); return entryFormId; } });