Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

How to call Await functions inside Realm.open function?

$
0
0

i was trying to integrate Realm db to my project

Model File : export const CHAT_LIST_SCHEMA = {  name: 'ImList',  properties: {    name: 'string',    rid: 'string',    lastMessage: 'string',    time: 'string',  },};
Code : init = async () => {    try {      Realm.open({ schema: CHAT_LIST_SCHEMA }).then((realm) => {        let cachedData = realm.objects('ImList');        console.log('Cached Data', cachedData);        if (cachedData === '') {          console.log('called123');          this.setState({ data: cachedData });        } else {          console.log('called');        const result = await RocketChat.getIMlist(); // API Call        const data = await RocketChat.getRoomsList(result); // Filtering          realm.write(() => {            data.map((items) => {              realm.create('ImList', {                name: items.name,                rid: items.rid,                lastMessage: items.lastMessage,                time: items.time,              });            });          });        }      });    } catch (error) {      console.log(error);    }  };

But it shows I cant call await outside an async function, but I only need to get data from the API, if the DB is empty. What to do?


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>