Alejandro Cordoba
Alejandro Cordoba

Reputation: 447

React findOne returning undefined on Client

I'm having problems using findOne because it always returns undefined.

This code:

Routine.js

Meteor.methods({
    .... // Some lines missing
    'routines.getRoutine'(routineId) {
        check(routineId, String);
        return Routines.findOne(routineId);
      },
});

Note: If I do a console.log of Routines.findOne(routineId) it correctly shows the element that i'm looking for.

App.jsx

  handleSubmit(event) {
    event.preventDefault();

    const comment = ReactDOM.findDOMNode(this.refs.comment).value.trim();
    Meteor.call('routines.addComment', this.state.routine._id, comment);
    let a = Meteor.call('routines.getRoutine', this.state.routine._id);
    ReactDOM.findDOMNode(this.refs.comment).value = '';
    this.setState({
      routine: a,
    });
  }

In my Appjs doesn't matter how I try 'a' is always undefined, what am I doing wrong?

Thanks for the help in advance!

Upvotes: 2

Views: 197

Answers (1)

coagmano
coagmano

Reputation: 5671

I'm pretty sure your problem is that Meteor calls on the client are async and so the method you're calling hasn't completed by the time you're querying the same data.

Try putting the rest of the code in the callback like so:

handleSubmit(event) {
    event.preventDefault();

    const comment = ReactDOM.findDOMNode(this.refs.comment).value.trim();
    Meteor.call('routines.addComment', this.state.routine._id, comment, function() {
        let a = Meteor.call('routines.getRoutine', this.state.routine._id);
        ReactDOM.findDOMNode(this.refs.comment).value = '';
        this.setState({
            routine: a,
        });
    });
}

Upvotes: 2

Related Questions