【问题标题】:MongoDB Callback ErrorMongoDB回调错误
【发布时间】: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; } });

标签: mongodb meteor callback


【解决方案1】:

根据Inserting and updating - MongoDB 提供的文档和示例
插入回调的第二个参数是插入的对象,在您的find 中,您正在寻找带有result 的文档,它应该是@ 987654324@ ,所以这应该可以工作:

EntryForms.find({_id: result._id}, function(err, res) {

【讨论】:

  • 我已经记录了插入回调的结果,它返回了插入的 EntryForms 对象的 _id。不幸的是,尝试您的解决方案产生了同样的错误。
  • hmm .. 抱歉没用,但你说你附上了错误信息,但我看不到它,也许如果你附上它,你可以获得更多帮助。
【解决方案2】:

原来问题与我在 Meteor 中发布/订阅我的对象的方式有关。我在我的 router.js 文件中注册了我的订阅,然后能够按预期访问我的收藏。将此归结为我对 Meteor 的小经历。

【讨论】:

    猜你喜欢
    • 2019-07-20
    • 2021-09-10
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多