React-redux + React-native-local-mongodb

时间:2018-05-27 23:38:52

标签: mongodb reactjs react-native redux react-redux

我在我的应用中使用react-native-local-mongodb和react-redux。数据库查询很慢,在状态更新后加载数据。

我使用componentDidMount使用Redux加载部分数据库,但结果很复杂。我无法替换状态,因为条件块(if else)在替换之前关闭,然后它会在以后返回我的数据。

在主页上,onLoadDatabase()正在调用正在加载缩减器的componentDidMount

componentDidMount() {
    console.log('1_componentDidMount');  
    this.props.onLoadDatabase();   
    console.log('8_state after loading db: '+this.props.list);
};

我在减速器中的功能是:

case LOAD_DATABASE:{
                console.log('2');
                let trainingsArray=null;
                mioDB.count({},(err,count)=>{
                    console.log('3_count:'+count);                    

                    if(count>=2){//it can't loads the database if there are two elements
                        console.log('4');
                        mioDB.find({},(err,trainings)=>{
                            console.log('5: '+JSON.stringify(trainings));
                            trainingsArray=trainings;
                        });

                    }else{//it must loads the database
                        console.log('4');
                        mioDB.insert([training1,training2],function(err,trainings){
                            console.log('5_');

                            mioDB.find({},(err,trainings)=>{
                                console.log('5.1: '+JSON.stringify(trainings));
                                trainingsArray=trainings;
                            });
                        });
                    }
                    console.log('6');
                    console.log('7.1'+state);
                    Object.assign({},state,{list:trainingsArray});                   
                });
                console.log('7.2'+state);

                return Object.assign({},state,{list:trainingsArray});                
            }break;

调试器输出:

Debugger

0 个答案:

没有答案