0

I am new to react-native and using realm database for my application. I want to pre-populate my data through json. I tried using componentDidMount() function and using for loop to insert data. Any guidence would be helpful.

Following is my code:

// 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 my index file, called componentDidMount :

 componentDidMount() {
    mountData().then().catch((error) => {
      alert(`Insert error ${error}`);
    });
  }

// I am getting 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)
2
  • Can you provide some example code of things you tried? Commented May 17, 2018 at 18:16
  • Hi Kevin, I have edited my question with example. Commented May 20, 2018 at 16:55

2 Answers 2

1

You can parse your JSON data to JavaScript objects but the objects have to match your schema.

let objs = JSON.parse(data);
realm.write(() => {
    objs.forEach((obj) => realm.create('data', obj));
}

where 'data' is the name of the schema.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you guys! I was making a very stupid mistake. In my schema file, while I was inserting a record in realm, I did not pass the name of my schema. Instead of databaseSchema I should pass the name of schema i.e BooksSchema.name (Books)

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(BooksSchema.name, {
                    id: Math.floor(Date.now() / 1000),
                    name: obj.name,
                    author: obj.author,
                    publisher: obj.publisher,
                }));
            });
        });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.