【问题标题】:Database "flapping" in Meteor.js?Meteor.js 中的数据库“抖动”?
【发布时间】:2013-12-20 10:08:50
【问题描述】:

正如 Abigail Watson 在thisgoogle 小组讨论中解释的那样,数据库抖动是 Meteor.js 中发生的一种现象,但没有详细说明。似乎出现了package 来解决这个问题。

所以,我的问题是,Meteor.js 中的“数据库抖动”是什么,以及在检查时它如何影响我,例如Deps.autorun() 中的 Meteor.user()?

编辑:我添加了一个让我头疼的代码示例,我认为这是由“数据库抖动”引起的。当用户登录时,Meteor.user() 中的console.logs 通常会运行几次,与注销console.logs 相同。在“userLoggedOut”内部,我正在重置 Meteor.user().profile.setAvailable 布尔值,有时这会让我陷入一个永恒的循环,因为 if(Meteor.user()) 会再次运行。这当然也可能是我做错了什么.. :)

我在使用这段代码时遇到了这个问题:

var lastUserId;
Deps.autorun(function () {
    if(Meteor.user()) {
        lastUserId = Meteor.user()._id;
        console.log("USER LOGGED IN WITH USER ID", lastUserId);
        if (Meteor.user().profile.setAvailable) {
            Meteor.call('setAvailable',
            {
                options: Meteor.user()
                               .profile
                               .someDataThatShouldBeSetAvailable
            });
        }
        if (Meteor.user().profile.forceLogOut) {
            Meteor.call('resetForceLogOut',
                {
                    userId: Meteor.user()._id
                });
            window.location.reload();
        }
    } else {
        if (lastUserId) {
            console.log("LOGOUT: We should do something
                with this id", lastUserId);
            Meteor.call('userLoggedOut',
                {
                    userId: lastUserId
                });
        } else {
            console.log("We don't have any lastUserId, the page 
                has probably been refreshed");
        }
    }
});

【问题讨论】:

  • 您的问题到底是什么?你遇到过这个问题吗?你想做什么?如果您提供预期结果、实际结果和一些重现实际结果的代码,您的问题会更容易回答。

标签: mongodb meteor database


【解决方案1】:

这是众所周知的,因为用户似乎是分阶段下班的。最初是它的null,然后是基本的(从延迟补偿.setUserId),然后从服务器接收实际数据。每次Deps.autorun 都会重新运行。

解决方法是检查配置文件,以便仅捕获最终运行

改变这一行

if(Meteor.user()) {

到这里

if(Meteor.user() && Meteor.user().profile && Meteor.user().profile.name

这里假设您的profile 中有一个.name 字段,但您可以使用任何字段。如果你有它,它可能是其他任何东西。这个想法是只等待有关用户的最后一块数据从服务器到达。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多