0

I do renderContacts and I'm trying to get results in a div with a contactResults id by calling renderContacts. It looks like that the problem is that the renderContacts() function doesn't wait for a Meteor.call and returns undefined right away. However, in the Meteor.call callback I get the right data from backend. So the problem only is that I don't know how I can make it to wait to a Meteor.call's callback.

class ContactsComponent extends React.Component {

    renderContacts() {

        return Meteor.call('parseClients', function(err, updatedContacts) {
            if (err) {
                console.log(err);
            } else {

                console.log(updatedContacts); // correct result in here

                if (updatedContacts.length) {
                    return contacts.map((contact, i) => (
                        <SingleContactRow key={contact._id} contact={contact}/>
                    ));
                }
            }
        });
    }

    render() {

        return (
            <div>

                <div>

                    <div id="contactResults">

                        {this.renderContacts()}

                    </div>

                </div>

            </div>
        );

    }
}

Here is the backend function:

'parseClients'() {
    return Contacts.findOne({});
  }

1 Answer 1

1

How about place the meteor call in componentDidMount and make use react state

class ContactsComponent extends React.Component {
    constructor () {
       super()
       this.state = { contacts: [] }
    }
    componentDidMount() {
        Meteor.call('parseClients', function(err, contacts) {
            if (err)
                console.log(err);

            if (contacts.length)
                this.setState({ contacts }
        });
    }

    render() {
        const { contacts } = this.state
        return (
            <div>
               <div id="contactResults">
                {contacts.map((contact, i) => <SingleContactRow key={contact._id} contact={contact}/>)}
               </div>
            </div>
        );

    }
}

Please take note, you cannot return async function of Meteor.call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.