andpor/react-native-sqlite-storage

Error in opening the database : openDatabase()

saadaoui-3 opened this issue · 2 comments

Error in opening the database

import { openDatabase } from 'react-native-sqlite-storage'

const db = openDatabase({ name: `${dbName}.db` })

Cannot read properties of undefined (reading 'open')

The SQLitePlugin function includes a method called open :
SQLitePlugin = function(openargs, openSuccess, openError) {
  ...
  this.open(this.openSuccess, this.openError);
};

SQLitePlugin.prototype.open = function(success, error) {
  var openerrorcb, opensuccesscb;
  ...
}

When the SQLitePlugin function is called, an error is thrown that says "Cannot read properties of undefined (reading 'open')."

The problem is caused by the fact that SQLitePlugin is defined as a function rather than a class. As a result, the open method is defined as a prototype method rather than a class method. When open is called, it's being called on an instance of the SQLitePlugin function, but the prototype method isn't being recognized.

Possible solution

To fix this issue, you've proposed converting SQLitePlugin into a class. Here's what the updated code would look like:

SQLitePlugin = class {
  constructor(openargs, openSuccess, openError) {
    ...
    this.open(this.openSuccess, this.openError);
  }
}

plugin.exec = function(method, options, success, error) {
  if (plugin.sqlitePlugin.DEBUG){
    plugin.log("SQLite." + method + "(" + JSON.stringify(options) + ")");
  }
  console.log(new SQLitePlugin(options,success,error))
  // NativeModules["SQLite"][method](options,success,error);
};

By converting SQLitePlugin into a class, should allow open to be recognized when it's called.
But still have another problem

After calling the "open" method i got another exception :

RangeError: Maximum call stack size exceeded at [Symbol.hasInstance] ()
_classCallCheck (...\node_modules@babel\runtime\helpers\classCallCheck.js:2:17)

I'm with same problem

Same Issue +1