1

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.

Nithin CV
  • 1,046
  • 9
  • 23
  • 1
    Best practice here on SO is to do some troubleshooting to narrow down the line of code that isn't working before posting questions. Please do some basic troubleshooting by adding a breakpoint and stepping through the code line by line, inspecting the vars and code execution until you spot something unexpected. For example, is your Realm file stored at the `olam1` path? If not, there's your problem. Another thing to try, immediately after opening the realm, write something to it, then read it back. Does that work? Update your question and we'll take a look. – Jay Oct 24 '22 at 20:36
  • @Jay : Yes you are right, I should dig deep, before posting the question. I issue was due to realm path. it was not throwing any issues to my invalid path. Docs are also poor related to path definition for NodeJS SDK. – Nithin CV Oct 27 '22 at 09:38

1 Answers1

1

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.

Nithin CV
  • 1,046
  • 9
  • 23