Mongodb Meteor-不再回调“Meteor”;“芬顿”;功能

Mongodb Meteor-不再回调“Meteor”;“芬顿”;功能,mongodb,meteor,Mongodb,Meteor,我正在从事一个Meteor项目,我必须说这一点都不容易,特别是在一件事上:回调 一切都是异步的,所以我想知道如何才能从mongodb获得结果 var user = Meteor.users.findOne({username: "john"}); return (user); // sometimes returns "undefined" 我不想脏兮兮的到处乱放。 有人能解决这个问题吗 编辑: 我注意到在router.js和console.log中,我的数据被返回了4次。2次未定义值,2

我正在从事一个Meteor项目,我必须说这一点都不容易,特别是在一件事上:回调

一切都是异步的,所以我想知道如何才能从mongodb获得结果

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

我不想脏兮兮的到处乱放。 有人能解决这个问题吗


编辑: 我注意到在router.js和console.log中,我的数据被返回了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");
    }
});
我在控制台上看到:

(以下为纯文字版)

findOne(yourId)
是一种同步方法,相当于
find({u id:yourId},callback)
。区别在于
find()
允许您定义回调。如果不将回调传递给
find()
此方法将是同步的

检查wrapAsync: 它允许您使用
async
操作以
sync
样式编码


EventedMind的免费课程:

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

如果您愿意,有meteor包可以使Mongodb异步(我还没有尝试过)

我想这种同步方法符合Mongodb的简单维护目标。想一想,我最讨厌使用Node的一个是异步回调瀑布/巢穴,它们的创建和维护非常困难。。。希望这能让我的代码更容易阅读、理解和修改

var future = new Future();

var _h = Hunts.findOne({huntId});

if(_h) {
       future.return(_h)
    } else {
        return future.wait();
    }
在server/startup.js上,您需要:
未来=新产品需求(“纤维/未来”)

我相当肯定
findOne
不是异步的,您的第一个示例应该很好。我假设您在客户端执行此操作,您希望确保您要查找的用户位于客户端集合中。在您的浏览器控制台中运行
Meteor.users.findOne({username:“john”})
,这就不需要同步了。findOne应该是同步的和被动的。您是说有时即使查询应该返回值,它也会返回未定义的值吗?注意,我通常只使用带有_id的findOne,这可能是我体验不同行为的部分原因。@LarryMaccherone是的,就是这样。它在应该返回其他内容的地方返回undefined。我注意到在console.log中,routes.js中的数据返回了4次:未定义的数据返回了2次,预期的对象返回了2次。但是在视图中,对象是未定义的。通过您的编辑,听起来好像有什么疯狂的事情正在发生。我在想,有些事情并不像你想象的那样。也许是范围问题?祝你好运,当你找到答案时请告诉我们,或者你是否可以发布更多代码来帮助我们。我放了一张图片和一些代码来让你理解。:)你好,谢谢。我会在明天的工作中通过实验让你不断更新。现在在Meteor 1.0上,find不再有回调功能。请检查我的编辑。
undefined
undefined
Object_id: "o3mgLcechYTtHPELh"addresses: (....)
Object_id: "o3mgLcechYTtHPELh"addresses: (....)
var future = new Future();

var _h = Hunts.findOne({huntId});

if(_h) {
       future.return(_h)
    } else {
        return future.wait();
    }