andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.75k stars 521 forks source link

Error in opening the database : openDatabase() #557

Closed saadaoui-3 closed 4 months ago

saadaoui-3 commented 1 year ago

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)

DouglasFJ commented 6 months ago

I'm with same problem

Ritesh175862 commented 5 months ago

Same Issue +1