Meteor-Community-Packages / ground-db

GroundDB is a thin layer providing Meteor offline database and methods
https://atmospherejs.com/ground/db
MIT License
572 stars 77 forks source link

Sometimes getting back undefined #202

Open LukasBlu1 opened 6 years ago

LukasBlu1 commented 6 years ago

Hello everyone,

I am working on a component that manages the add and edit form for customer data. From the URL I get the id and load the data in the form. If there is no data it should display a message and changes the state to add mode. The problem is sometimes I get back undefined and right after I get the data, so I can't distinguish if the data will arrive very soon or if the record is deleted.

Many thanks in advance. Lukas

Api

let CustomersApi;
if (Meteor.isClient) {
    CustomersApi = new Ground.Collection('customers');
} else {
    CustomersApi = new Mongo.Collection('customers');
}

export default CustomersApi;

Pass data to component

export default createContainer((props) => {
    if(props.customerId){
        return {
            customer: CustomersApi.findOne({_id: props.customerId})
        };
    } else {
        return {}
    }
}, CustomerAddEdit);

Proccess received data

componentWillReceiveProps(nextProps){
        if(this.state.editMode){
            if(nextProps.customer){

                this.setState({
                    customerData: nextProps.customer,
                    isFetching: false
                });

            } else {

                UiMessagesController({
                  type: 'warning',
                  header: 'Kunde nicht verfügbar',
                  messages: '...'
                })

                this.setState({
                    editMode: false,
                    isFetching: false
                });

            }
        }
    }
tillk9 commented 6 years ago

i have a similar problem. it seems as if the isReady function were called too fast.