Error when synchronizing data from local database implemented with watermelonDB in APP React-Native

Asked

Viewed 13 times

0

I was recently developing an app that simulated car rental. In one of the steps of the app, I should implement a local database that every time the user was online, it would take the API data and save it locally, thus allowing the user offline access. If any changes were made by the user, when I was connected to the internet again, the app would automatically synchronize the data, which would be done by:

async function offlineSincronize(){
    await synchronize({
      database,
      pullChanges: async ({ lastPulledAt }) => {
        
        const response =  await api
        .get(`cars/sync/pull?lastPulledVersion=${lastPulledAt || 0}`);
        
        const { changes, latestVersion } = response.data;
        console.log(changes.cars)
        return {changes, timestamp: latestVersion}
      },
      pushChanges: async ({ changes }) =>{
       
        const user = changes.users;
        await api.post('/users/sync/', user)

      }
    });
  }
 

The hope is that the pullChanges takes the API data and writes it to the database locally, which I believe should be done by Return. Already push, send the new data, if there was any change in them. Only when it comes to testing useEffect(() =>{ Let isMounted = true; async Function fetchCars(){ Try{

       // const carsFetched = await api.get('/cars');
       const carCollection = database.get<ModelCar>('cars')
       const cars = await carCollection.query().fetch();
      //  console.log('vindo do DB')
      //  console.log(carsFetched);
       // const carsFetched = await carCollection.query().fetch();
        if(isMounted){
          setCars(cars);
        }
      }catch(error){
        console.log(error);
      }finally{
        if(isMounted){
          setLoading(false);
        }
        
      }
    }
    fetchCars();
    
    return () =>{
      isMounted = false;
    };
  }, []);
  useEffect( ()=>{
    if(netInfo.isConnected){
      offlineSincronize();
    }
  }, [netInfo.isConnected]);

I get from my database an empty vector, and by the tests I have impementei had the impression that for some reason my App is not writing in my local database although the API is providing the data correctly. Could someone help me with this question? I’ve been trying for a long time and I can’t solve it.

inserir a descrição da imagem aqui

The complete project can be accessed at: https://github.com/lucasvittal2/rentX

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.