What I'm trying to do is configure a Realm in React Native. I have a class defining the schema of the object I want to persist in realm/mongodb.
import {Realm, createRealmContext} from '@realm/react';
import { Configuration, ConfigurationWithSync } from 'realm';
import {appId} from "../atlasConfig.json";
const app = Realm.App.getApp(appId);
const { currentUser } = app;
export class Task extends Realm.Object {
_id!: Realm.BSON.ObjectId;
description!: string;
isComplete!: boolean;
createdAt!: Date;
count!: number;
static generate(description: string) {
return {
_id: new Realm.BSON.ObjectId(),
description,
isComplete: false,
createdAt: new Date(),
count: 0
};
}
static schema = {
name: 'Task',
primaryKey: '_id',
properties: {
_id: 'objectId',
description: 'string',
isComplete: {type: 'bool', default: false},
createdAt: 'date',
count: 'int?'
},
};
}
const config: Configuration = {
schema: [Task],
schemaVersion: 0,
onMigration: (oldRealm: Realm, newRealm: Realm) => {
if (oldRealm.schemaVersion < 1){
const oldObjects = oldRealm.objects(Task);
const newObjects = newRealm.objects(Task);
for(const index in oldObjects){
const oldObject = oldObjects[index];
const newObject = newObjects[index];
if(oldObject.isComplete){
newObject.count = 1010;
} else {
newObject.count = 2020;
}
}
}
},
sync : {
flexible: true,
onError: console.error,
initialSubscriptions: {
update: (subs: Realm.App.Sync.MutableSubscriptionSet, realm: Realm) => {
subs.add(realm.objects('Task'));
},
rerunOnOpen: true
}
}
};
export const realmContext = createRealmContext(config);
What I'm having trouble with is creating the configuration for the Realm. I want to add the sync config in the realm config, but it doesn't seem to work. Before, I gave the sync config to the RealmProvider as a prop. It didn't give me any error but it didn't do what I wanted. That's why I thought it would be better to create the config in the Realm config directly.
The error I'm getting is in the "sync" config in the realm config I'm declaring in the class above. It says that it is missing the "user" property, but when I add this property, I don't know what value to give it because whatever I try, the user seems to be null at this point. I couldn't find anything that could help me anywhere. And in all the docs I found on MongoDB website, they use the "user" property without giving it any value.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/wbkqJ68
Comments
Post a Comment