mbvissers / RealmExampleApp

7 stars 3 forks source link

[Log] AsyncStorage(ERROR)=======>>>>> – Error: Realm object constructor must not return another value — index.bundle:101591 (index.bundle, line 112460) #1

Open Vivaan7 opened 3 years ago

Vivaan7 commented 3 years ago

hello I got this error in my code when I try to add any detail in particular schema .. help me very soon

mbvissers commented 3 years ago

Hi, normally I won't reply to issues on my blog-related repo's since most information would be in the blog itself, or could be resolved with a quick Google search. But I'll try to point you in the right direction.

It is also very hard to debug without your code itself, how similar it might be to mine. I'm also very rusty on Realm so might not be of much help.

If you're adding more details to an already-used schema, you should update the schemaVersion. If that is not the issue, I found a similar issue on the Realm-js repo that you can find here.

The issue in that thread was that you shouldn't extend from Realm.Object{} (Like I did) and that you should try to just have the class of a schema without the extend.

Hope it helps.

Vivaan7 commented 3 years ago

// import { asyncStorageKeys, storeAsyncData } from "../api/AsyncstorageFunction";

import { Alert } from "react-native"; import Realm from "realm";

// Declare Convo class ConvoSchema extends Realm.Object { } ConvoSchema.schema = { name: 'Conversations', primaryKey: 'id',

properties: {
    id: { type: 'int', default: 0 },
    sender_id: 'string',
    sender_jid: 'string',
    receiver_id: 'string',
    receiver_jid: 'string',
    sender_profile: 'string',
    sender_name: 'string',
    receiver_profile: 'string',
    receiver_name: 'string',
    message_type: 'string',
    last_message: 'string',
    last_message_time: 'string',
    unread_message: 'string',
    insertdate: 'string',
},

};

// Messages schema class MessageSchema extends Realm.Object { } MessageSchema.schema = { name: 'Messages', primaryKey: 'id',

properties: {
    id: { type: 'int', default: 0 },
    chat_id: 'string',
    sender_id: 'string',
    message_type: 'string',
    message: 'string',
    duration: 'string',
    insertdate: 'string',
},

};

// Create realm let realm = new Realm({ schema: [ConvoSchema, MessageSchema], schemaVersion: 4 }); // let realm = Realm.open({ // path: "MyDatabase", // schema: [ConvoSchema,MessageSchema], // });

// Functions // Return all convo let getAllConvo = () => { return realm.objects('Conversations'); };

// Add a single convo using parameters let addConvo = (sender_id, sender_jid, receiver_id, receiver_jid, sender_profile, sender_name, receiver_profile, receiver_name, message_type, last_message, last_message_time, unread_message, insertdate) => { realm.write(() => { let _id = 0; if (getAllConvo().length > 0) _id = realm.objects('Conversations').max('id') + 1; const convo = realm.create('Conversations', { id: _id, sender_id: sender_id, sender_jid: sender_jid, receiver_id: receiver_id, receiver_jid: receiver_jid, sender_profile: sender_profile, sender_name: sender_name, receiver_profile: receiver_profile, receiver_name: receiver_name, message_type: message_type, last_message: last_message, last_message_time: last_message_time, unread_message: unread_message, insertdate: insertdate, }); }); }

// Remove all convo from Realm database let deleteAllConvo = () => { realm.write(() => { realm.delete(getAllConvo()); }); };

// Update all convo that have a null value as edition and update it to 1 let updateAllConvoEditions = () => { realm.write(() => { let convo = getAllConvo() convo.map((item, index) => { if (item.edition === null) { item.edition = 1 } }) }); };

// Get all convo with more than 2 years using .filtered() let getBigConvo = () => { return realm.objects('Conversations').filtered('insertdate > 02/02/2018'); }

// Get all messages let getAllMessages = () => { return realm.objects('Messages'); };

// Add a single message using parameters let addMessage = (chat_id, sender_id, message_type, message, duration, insertdate) => { realm.write(() => { let _id = 0; if (getAllMessages().length > 0) _id = realm.objects('Messages').max('id') + 1;

    const message = realm.create('Messages', {
        id: _id,
        chat_id: chat_id,
        sender_id: sender_id,
        message_type: message_type,
        message: message,
        duration: duration,
        insertdate: insertdate,
    }, () => {

        // await storeAsyncData(asyncStorageKeys.generateMessages, 'true');

    });
});

}

// Remove all messages from Realm database let deleteAllMessages = () => { realm.write(() => { realm.delete(getAllMessages()); }); };

// Get Message by id let getMessageById = (_id) => { return realm.objects('Messages').filtered(id = ${_id}); }

// Exports // Export the realm so other files can access it export default realm;

// Export other functions so other files can access it export { getAllConvo, addConvo, deleteAllConvo, updateAllConvoEditions, getBigConvo, getAllMessages, addMessage, getMessageById, deleteAllMessages }

Vivaan7 commented 3 years ago

@mbvissers above is my database schema and methods for usage I was facing issue about realm so I tried with open as well as I made comment there now .... apart from that can you tell me how I can test ur demo as well because when I tried to run your demo I was facing issue in iOS and android as well ....