Meteor + react and "undefined" error


#1

Im using Meteor + React and “this.props.thing.source” is a string for a mongodb _id.

As you can see it works fine when I pass in the string of the ID itself, but I get an undefined error when passing in the variable, even though that variable renders out that same string.

In this code:

Thing = React.createClass({
  propTypes: {
    thing: React.PropTypes.object.isRequired
  },
  render() {
    return (
      <ul>
        <li>Display: {Things.findOne(this.props.thing.source).data}</li>
        <li>Display: {Things.findOne("emq6M4WbJeRvkA6Q3").data}</li>
        <li>Source: {this.props.thing.source}</li>
      </ul>
    );
  }
});

This does NOT work:

Display: {Things.findOne(this.props.thing.source).data}

This works:

Display: {Things.findOne(“emq6M4WbJeRvkA6Q3”).data}

And this correctly renders “emq6M4WbJeRvkA6Q3”:

Source: {this.props.thing.source}

The ERROR I am getting:

“Uncaught TypeError: Cannot read property ‘data’ of undefined”


#2
 Things.findOne(this.props.thing.source).data

I wonder if you need to explicitly define the query field, {Things.findOne({id_: this.props.thing.source}).data}

or explicitly force this.props.thing.source to be a String primitive?


#3

just tried a couple things trying to make sure / enforce that it is a string and get same error, so im thinking it is passing as a string fine. it also renders correctly as a Mongo _id.

the findOne() function I checked and doesnt need the key, only the find() function does.


#4

just figured it out, and it is related to the string … i found out not all of the Things has a value for .source, so on some it was undefined … now im checking if there is a string value first, and only then performing the find.