Sorry that you’re having issues and without knowing exactly what you’re building, I can’t be specific, but what I would say is that you should look at the newer docs here - https://docs.mongodb.com/realm/node/sync-data/#node-sync-data and perhaps follow the steps there?
Well, I’m using realm io DB for now,
We have an application built under realm.io and another DB in mongo realm, So what we want to do is, to get changes under realm.io DB and copy into Mongo realm DB.
I think the one you shared to me is to get the changes from mongo realm DB not form realm io DB.
'use strict';
const fs = require('fs');
const Realm = require('realm');
// the URL to the Realm Object Server
const SERVER_URL = '//instance.cloud.realm.io:9080';
const adminCreds = Realm.Sync.Credentials.usernamePassword('Administrator', 'password', false);
// The regular expression you provide restricts the observed Realm files to only the subset you
// are actually interested in. This is done in a separate step to avoid the cost
// of computing the fine-grained change set if it's not necessary.
var NOTIFIER_PATH = '.*';
//declare admin user
let adminUser = undefined
// The handleChange callback is called for every observed Realm file whenever it
// has changes. It is called with a change event which contains the path, the Realm,
// a version of the Realm from before the change, and indexes indication all objects
// which were added, deleted, or modified in this change
var handleChange = async function (changeEvent) {
console.log("this is a change");
}
function verifyCouponForUser(coupon, userId) {
//logic for verifying a coupon's validity
}
async function login(serverUrl,user) {
let result = await Realm.Sync.User.login(serverUrl,user);
return result;
}
// register the event handler callback
async function main() {
try{
adminUser = await login('https://instance.cloud.realm.io',adminCreds);
Realm.Sync.addListener(`realms:${SERVER_URL}`, adminUser, NOTIFIER_PATH, 'change', handleChange);
}catch(e) {
console.log('error');
console.log(e);
}
}
main()
That’s all. and I got this: “Sync connection was not fully established in time” after 2 minutes. in console.
I add an element to the realm and try to save the response into a file.txt but nothing happens.
It suppose, if I set NOTIFIER_PATH = ‘.*’; it will listen to all realms already created for updates.
Thanks @Ian_Ward remove the port worked but was not working with HTTPS so I keep “realms:” and it worked that way.
I read the documentation to get the data of the changes but I couldn’t get it to work that way so this is the code that it wrote and worked for me
'use strict';
const fs = require('fs');
const Realm = require('realm');
// the URL to the Realm Object Server
const SERVER_URL = '//instance.cloud.realm.io';
const adminCreds = Realm.Sync.Credentials.usernamePassword('Administrator', 'password', false);
const NOTIFIER_PATH = './mainRealm';
var handleChange = async function (changeEvent) {
let realm = changeEvent.realm;
let users = realm.objects('User');
const userInsertIndexes = changeEvent.changes.User.insertions;
const userModIndexes = changeEvent.changes.User.modifications;
const userDeleteIndexes = changeEvent.changes.User.deletions;
console.log(userInsertIndexes[0].userId);
console.log(userInsertIndexes[0].name);
console.log(userInsertIndexes[0].age);
console.log(userInsertIndexes[0].isDone);
console.log(userInsertIndexes[0].timestamp);
saveinfo(userInsertIndexes[0]);
}
function saveinfo(obj){
try {
let jsonResponse = JSON.stringify(obj, null, 2);
console.log("\nFile Contents of file before append:",
fs.readFileSync("/test.txt", "utf8"));
fs.appendFileSync('/test.txt',jsonResponse, (err) => {
if (err) throw err;
});
}catch (e) {
console.log(e);
}
}
async function login(serverUrl,user) {
let result = await Realm.Sync.User.login(serverUrl,user);
return result;
}
async function main() {
try{
let adminUser = await login('https://instance.cloud.realm.io',adminCreds);
Realm.Sync.addListener(`realms:${SERVER_URL}`, adminUser, NOTIFIER_PATH, 'change', handleChange);
}catch(e) {
console.log('error');
console.log(e);
}
}
main()