【问题标题】:Meteor - no more callbacks for "findOne" functionMeteor - “findOne”函数不再回调
【发布时间】:2014-12-01 19:18:47
【问题描述】:

我正在做一个 Meteor 项目,我必须说这根本不容易,尤其是对于一件事:回调!

一切都是异步的,所以我想知道我必须如何从我的 mongodb 中获取结果。

var user = Meteor.users.findOne({username: "john"});
return (user); // sometimes returns "undefined"

...

var user = Meteor.users.findOne({username: "john"});
if (user)                    // so ok, I check if it exists!
    return (user);           // Cool, I got my user!
return ();                   // Ok and what should I return here? I want my user!

我不想脏兮兮地把 setTimeout 之类的东西放在任何地方。 有人有这个解决方案吗?


编辑: 我在带有 console.log 的 router.js 中注意到我的数据被返回了 4 次。 2 次为未定义值,另外 2 次为预期值。在视图中,它仍然是未定义的。 为什么路由器在这条路线上通过了 4 次?是否在路由器中显示返回值的第一个结果?

如果 find() 没有找到任何东西,我应该返回什么?


编辑 2:这是一些需要理解的代码。

this.route('profilePage', {
    path: 'profil/:_id?',
    waitOn: function() {
        return [
        Meteor.subscribe('article', { prop: this.params._id}), // id can be id or username
        Meteor.subscribe('article', { userId: this.params._id}), // id can be id or username
        Meteor.subscribe('params'),
        Meteor.subscribe('profil', (this.params._id ? this.params._id : Meteor.userId()))
        ];
    },
    data: function() {
        if (this.params._id) {
            var user = Meteor.users.findOne(this.params._id);
            if (!user)
                user = Meteor.users.findOne({username: this.params._id});
            console.log(user);
            return user;
        }
        else if (Meteor.userId())
            return Meteor.user();
        else
            Router.go("userCreate");
    }
});

我在控制台上得到了这个: http://puu.sh/debdJ/69419911f7.png

(文字版本如下)

undefined
undefined
Object_id: "o3mgLcechYTtHPELh"addresses: (....)
Object_id: "o3mgLcechYTtHPELh"addresses: (....)

【问题讨论】:

  • 我很确定findOne 不是异步的,您的第一个示例应该没问题。我假设您在客户端上执行此操作,您希望确保您正在寻找的用户在您的客户端集合中。在您的浏览器控制台中运行Meteor.users.findOne({username: "john"}),这应该可以解决同步问题。
  • findOne 应该是同步和反应式的。你是说即使查询应该返回一个值,有时它也会返回 undefined ?请注意,我通常只使用带有 _id 的 findOne,这可能是我遇到不同行为的部分原因。
  • @LarryMaccherone 是的,就是这样。它返回 undefined 它应该返回其他东西的地方。我注意到 console.log 我在 routes.js 中的数据被返回了 4 次:2 次未定义,2 次返回我期望的对象。但是在视图中,对象是未定义的。
  • 通过您的编辑,听起来像是发生了一些疯狂的事情。我在想某些东西并没有像你想象的那样连接起来。也许是范围问题?祝你好运,当你弄清楚或者你是否可以发布更多代码来帮助我们时告诉我们。
  • 我放了一张图片和一些代码让你理解。 :)

标签: mongodb meteor


【解决方案1】:

findOne(yourId) 是一种同步方法,相当于find({ _id: yourId}, callback)。不同之处在于find() 允许您定义回调。如果您不向find() 传递回调,此方法将被同步。

检查 wrapAsync:http://docs.meteor.com/#/full/meteor_wrapasync 它允许您使用 async 操作以 sync 样式进行编码。

EventedMind 的免费课程:https://www.eventedmind.com/feed/meteor-meteor-wrapasync

【讨论】:

  • 您好,谢谢。明天我会在工作中进行试验,让您了解最新情况。
  • 现在在 Meteor 1.0 上,find 不再有回调。请检查我的编辑。
【解决方案2】:

到目前为止,我的经验是 Meteor Mongodb 包中的函数通常不提供回调(由于某种原因 insert 会...),这些函数是原子的(因此是同步的)。

如果你愿意,有一些流星包可以使 Mongodb 异步(我没有尝试过)。

我猜这种同步方式符合 Mongodb 的简单维护目标。想一想,我在使用 Node 时最讨厌的一个问题是使用异步回调瀑布/嵌套,创建和维护它们很痛苦……希望这将使我的代码更易于阅读、理解和更改……

【讨论】:

    【解决方案3】:
    var future = new Future();
    
    var _h = Hunts.findOne({huntId});
    
    if(_h) {
           future.return(_h)
        } else {
            return future.wait();
        }
    

    在 server/startup.js 你需要: Future = Npm.require('fibers/future');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多