即使数据存在,findOne也会抛出undefined

时间:2016-01-16 17:55:50

标签: javascript mongodb meteor react-router minimongo

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

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

随机工作。有时我会得到正确的主题,有时候在读取themeColor时会抛出未定义的内容,即使数据存在。我可以通过console.log看到它总是得到正确的ID,但仍然findOne可以抛出undefined。当我更改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()
    }

},

1 个答案:

答案 0 :(得分:0)

由于代码有时正在运行。可能存在一些与架构不匹配的测试数据。所以测试集合中的所有数据。

相关问题