5

I'm new to Meteor so I've been playing around and now I'm stuck with this problem.

I'm using React Router to try to show a theme based in the URL /(:userId). If there's no userId inserted into the URL it should show the current user's theme and If there's no current user it should show a default theme.

It's working randomly. Sometimes I get correct theme, sometime it throws undefined when reading themeColor even though the data is there. I can see with console.log that it always get the right Id, but still findOne can throws undefined. It specially happens when I change the URL (/xyz) and go back to the default one (/).

I verified with the console that userId is the actual owner of themeColor and themeTextColor.

I'm using React, React-router, autopublish. I removed insecure.

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

},
hiei
  • 53
  • 4
  • 2
    Where are you subscribing to that data? I guess you need to wait until subscription is ready. – dr.dimitru Jan 16 '16 at 18:04
  • I'm using autopublish for now. – hiei Jan 16 '16 at 19:29
  • 3
    Autopublish simply publishing all data, but not waiting for it. Remove autopublish and use publish/subscribe. Best option is routing with build-in wait and subscription options like: [flow-router](https://github.com/VeliovGroup/flow-router) or [iron-router](https://github.com/iron-meteor/iron-router) – dr.dimitru Jan 16 '16 at 20:21

1 Answers1

0

Since the code is working sometime. There can be some test data which doesn't match the schema. So test all data in the collection.

Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58