Recently, I started working with react-native and decided to incorporate realm database into my application. My goal is to pre-populate the data using a JSON file. Initially, I attempted to achieve this by utilizing the componentDidMount() function along with a for loop for data insertion. If anyone has any advice or guidance on this matter, it would be greatly appreciated.
Below is the code snippet I have written:
// Schema File:
import booksdata from './books.json';
const Realm = require('realm');
const BooksSchema = {
name: 'Books',
primaryKey: 'id',
properties: {
id: 'int', // primary key
name: 'string',
author: 'string',
publisher: 'string',
},
};
const databaseSchema = {
path: 'books.realm',
schema: [BooksSchema],
schemaVersion: 0, // optional
};
export const mountData = () => new Promise((resolve, reject) => {
Realm.open(databaseSchema)
.then((realm) => {
// Create Realm objects and write to local storage
realm.write(() => {
booksdata.forEach(obj => realm.create(databaseSchema, {
id: Math.floor(Date.now() / 1000),
name: obj.name,
author: obj.author,
publisher: obj.publisher,
}));
});
});
});
// In the index file, I call componentDidMount :
componentDidMount() {
mountData().then().catch((error) => {
alert(`Insert error ${error}`);
});
}
// However, I am encountering the following warning:
Possible Unhandled Promise Rejection (id: 0):
Error: objectType must be of type 'string', got ([object Object])
Error: objectType must be of type 'string', got ([object Object])
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62352:45)
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62385:24)
at Object.callMethod (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62128:22)
at Realm.<anonymous> (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61983:28)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61408:15
at tryCallOne (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16056:14)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16157:17
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2884:21
at _callTimer (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2773:9)
at _callImmediatesPass (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2809:9)