FindOne throws undefined, although there is data

I'm new to Meteor, so I played, and now I'm stuck with this problem.

I am using React Router to try to show a theme based on URL / (: userId). If no userId is added to the URL, it should show the current user theme, and if there is no current user, it should show the default theme.

It works randomly. Sometimes I get the right theme, sometime it throws undefined when reading themeColor, even if there is data. I can see from console.log that it always gets the correct identifier, but still findOne can call undefined. This happens when I change the URL (/ xyz) and return to the standard (/).

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

I am using React, React-router, autopublish. I retired unsafe.

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() } }, 
+5
source share
1 answer

Since the code does not work once. There may be some test data that does not fit the circuit. Therefore, check all the data in the collection.

0
source

Source: https://habr.com/ru/post/1240764/


All Articles