findOne dose not work as expected


#1

How can i retrieve only one entry? ,I have this this

import React, { Component } from 'react';
import { createContainer } from 'meteor/react-meteor-data';

//api entries
import { muscleGroups } from '../../api/muscleGroups.js';

export default class editMuscleGroup extends Component {
    getMuslceGroup(){
        return (
            <span>{this.props.muscleGroup.type}</span>
        )
    }
    render (){
        return <div>--{this.getMuslceGroup()}--</div>;
    }
}
editMuscleGroup.propTypes = {
    muscleGroups: React.PropTypes.object
};

export default createContainer((params) => {
    return {
        muscleGroup:muscleGroups.findOne({type:params.params.type})
    };
}, editMuscleGroup);
import { Mongo } from 'meteor/mongo';
export const muscleGroups = new Mongo.Collection('muscleGroups');

but is not working for me , I get undefined on this.props.muscleGroup.type

I use latest version of Meteor , with react
params.params.type is from the Link component

What I want is to use an link to transfer the user to an editing page of a single muscleGroup
When I click from the list of muscleGroups I get the expected result but if I refresh I get the TypeError: Cannot read property ‘type’ of undefined"
what I am missing ?
tanks in advance


#2

Are you sure params.params.type is not undefined?


#3

No “params.params.type” is defined , its served by <Link to="/muscleGrups/edit/chest" /> reactive component.
params.params.type is “chest” , as expected

the error is not in

 muscleGroup:muscleGroups.findOne({type:params.params.type})

is on this line

<span>{this.props.muscleGroup.type}</span>

#4

How are you subscribing to your data - are you using autopublish?


#5

yes , I do use autopublish (I am learning the meteor platform, so I do not use the publishing and subscription methods)


#6

So I figured out , the idea is that the data that we get with findOne is not available at the moment of the page refresh , so I instantiate the muscleGroup with an empty object that is automatically updated when the data is ready

export default createContainer((params) => {
    Meteor.subscribe('muscleGroups');
    return {
        muscleGroup:muscleGroups.findOne({type:params.params.type})||{}
    };
}, editMuscleGroup);

they rely need to specify things like this up front .
because I think this may be a common problem
tanks hwillson and jhuenges for the interest