Meteor findOne not returning value


#1

I have a component that has a subscription to items.
The items are confirmed to be there as if I use the following it returns the cursor with the item inside it:

console.log(Items.find({key: itemKey}).fetch());

The items collection is imported etc.

My constructor:

constructor(){
            super();
            this.state = {
                    subscription: {
                            items: Meteor.subscribe("allItems")
                    }
            }
    }

my component will unmount method:

    componentWillUnmount(){
            this.state.subscription.items.stop();
    }

This is the methods where the issue lies. The error is Uncaught TypeError: Cannot read property ‘srcEntire’ of undefined:

    getItemSource(itemKey){
           console.log(itemKey); // shows the correct key
           var itemer = Items.findOne({key: itemKey});
           console.log(itemer.srcEntire);
            return itemer.srcEntire;
    }

The render section ( It shows the image if I hardcode the return value of the getItemSource method. The source of the image is found by calling the getItemSource method and passing in the layer prop that is passed in (props work):

    render(){	


     return (  								
              <div className="col-lg-3" >
                           
             <img id="myLayer" className="on-top img-responsive center-block on-top " name="myLayer" src={this.getItemSource(this.props.layers.myLayer)} />

</div>

)}

#2

Is there any place you wait for the subscription to be ready?


#3

No, is there a way to do this?


#4

I would strongly recommend against subscribing in the constructor. Rather, wrap your component in a Meteor container and follow that pattern.

See here.