【问题标题】:findOne throwing undefined even though the data is there即使数据在那里,findOne 也会抛出 undefined
【发布时间】:2016-01-16 17:55:50
【问题描述】:

我是 Meteor 的新手,所以我一直在玩,现在我遇到了这个问题。

我正在使用 React Router 来尝试显示基于 URL /(:userId) 的主题。如果 URL 中没有插入 userId,则应显示当前用户的主题,如果没有当前用户,则应显示默认主题。

它随机运行。有时我得到正确的主题,有时在读取 themeColor 时它会抛出 undefined 即使数据在那里。我可以通过 console.log 看到它总是得到正确的 ID,但 findOne 仍然可以抛出未定义的。当我更改 URL (/xyz) 并返回到默认 URL (/) 时,会特别发生这种情况。

我通过控制台验证了 userId 是 themeColor 和 themeTextColor 的实际所有者。

我正在使用 React、React-router、自动发布。我删除了不安全的。

getMeteorData() {

    var currentViewedPageId = this.props.params.userId? this.props.params.userId:(Meteor.userId()?Meteor.userId():false);
    console.log(currentViewedPageId); //Allways right
    console.log(Personalization.findOne({owner: currentViewedPageId}).themeColor); //Sometimes undefined, sometimes not
    if(currentViewedPageId)
    {

        return {
        currentUser: Meteor.user(),
        themeColor: Personalization.findOne({owner: currentViewedPageId}).themeColor,
        themeTextColor: Personalization.findOne({owner: currentViewedPageId}).themeTextColor
        };
    }
    return {
        currentUser: Meteor.user()
    }

},

【问题讨论】:

  • 您在哪里订阅这些数据?我想你需要等到订阅准备好。
  • 我现在正在使用自动发布。
  • 自动发布只是发布所有数据,而不是等待它。删除自动发布并使用发布/订阅。最佳选择是使用内置等待和订阅选项进行路由,例如:flow-routeriron-router

标签: javascript mongodb meteor react-router minimongo


【解决方案1】:

由于代码有时会起作用。可能有一些与架构不匹配的测试数据。所以测试集合中的所有数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2021-11-03
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多