RealmDB results always showing empty

219 Views Asked by At

For a hobby project, I was trying to create and access the realm DB local using NodeJS SDK. I've created the DB (.realm file) with realm studio and CSV file, and able to browse the objects.

enter image description here

However, when I tried to connect with NodeJS SDK, I'm unable to query the data and DB instance always returning empty.

I've referred the documentation and other stack-overflow questions, but unable to find the root cause.

This is my code,

const Realm = require('realm');

const OlamDBSchema = {
  name: 'OLAM_DB',
  primaryKey: '_id',
  properties: {
    _id: 'string',
    english_word: 'string',
    part_of_speech: 'string?',
    malayalam_definition: 'string',
  },
};
async function execute() {
  try {
    const realm = await Realm.open({
      path: 'olam1', // olam1.realm file in same folder
      schema: [OlamDBSchema],
    });

    let dict = realm.objects('OLAM_DB');
    let result = dict.filtered("english_word == 'Abduct'"); // word exists
    console.log(result.length, realm.isClosed, realm.isEmpty); // always prints 0 false true
    realm.close();
  } catch (error) {
    console.error(error);
  }
}

execute().then(() => {});

I'm using OSx 12.6, NodeJS 16.14 & realm 11.0.0

Could you please point out, what I'm doing wrong here. Any suggestions or feedbacks will be helpful. Thank you.

1

There are 1 best solutions below

0
Nithin CV On

I've found the issue in my code. Sharing to you, if anybody gets into such a situation.

 const realm = await Realm.open({
      path: 'olam1', // path was given wrong, but SDK did not throw any error
    });

when I changed to a correct file path, everything worked OK.