I have a collection MyCollection which has documents with a title property and I want - in a component - to get the title for an id which the component got in a property and I keep getting errors on these attempts.
it gets the correct title but gives the error in promise “Cannot read property ‘title’ of undefined” and returns an object Promise instead of the title.
How can I make sure - without throwing errors - that all needed data are available before the component gets rendered?
I am using Vue for the front end with vuetify for the parent component. The parameter id is extracted from the page URL with routes and passed to the component as a property.
When I change the code to
the console shows that on page reload the method is called 3 times - each time the id parameter is correct but the first 2 times it shows the error Cannot read property ‘title’ of undefined. The 3rd time the title is retrieved and shown.
The parent component has in it’s data
currentId: this.$route.params.id,
The component is called in the parent component as
I observe that on page reload the parent component first renders as if it doesn’t get it’s data from the collection and then automatically re-renders showing the correct data - but without throwing an error.
I use neither autopublish nor insecure. I explicitely publish these data to all and I have methods to insert, update and delete items in the collection but not for finding.
I am not familiar how it is done with Vue but your queries must be inside a tracker compute function. But nevertheless, the explanation to why you are seeing an empty result initially is as I have posted above
I noted (in a re-structured version of my code to avoid effects from router) that the error occurs in particular when the user clicks the reload button of the Chrome browser. Then the Meteor client seems to loose the connection to the database for a short time.
How can this be avoided?
There is no way to avoid it. Reload will result for meteor to start again and thus lose all data in minimongo. You need a way to consider that subscription takes time to populate minimongo with data and therefore the initial render of your components will not have any data available. And then have your component re-render again once the data becomes available.
Sorry, I am not that familiar with how this is done with Vue