findOne 抛出未定义,即使数据在那里

findOne throwing undefined even though the data is there

本文关键字:数据 在那里 未定义 findOne      更新时间:2023-09-26

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

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

它是随机工作的。有时我得到正确的主题,有时它在读取主题颜色时抛出未定义,即使数据在那里。我可以看到控制台.log它总是得到正确的 Id,但仍然发现一个可以抛出未定义。当我更改 URL (/xyz) 并返回到默认 (/) 时,它特别发生。

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

我正在使用React,

React-router,autopublish。我删除了不安全感。

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()
    }
},

由于代码在某个时候起作用。可能存在一些与架构不匹配的测试数据。因此,请测试集合中的所有数据。