andpor / react-native-sqlite-storage

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

Transaction callback error: Cannot read property 'then' of undefined #390

Open ezranbayantemur opened 5 years ago

ezranbayantemur commented 5 years ago

I tried to catch transaction callback but it gives undefined error. (I'm passing the initial codes, define db, openDatabase etc..)

  componentDidMount() {
    const { db } = this.state;

    db.transaction(tx => {
      tx.executeSql('SELECT * FROM tblUser;', [], (tx, results) => {
        const rows = results.rows;
        let users = [];

        for (let i = 0; i < rows.length; i++) {
          users.push({
            ...rows.item(i),
          });
        }

        this.setState({ users });
      },
      )
    }).then(() => console.log("TRANSACTION DONE"))  // <== GIVES ERROR
  }

Error output:

C:\Users\ezran\Desktop\TestingProjects\testProject\node_modules\react-native\Libraries\Core\ExceptionsManager.js:44 TypeError: Cannot read property 'then' of undefined

This error is located at:
    in App (at renderApplication.js:40)
    in RCTView (at AppContainer.js:101)
    in RCTView (at AppContainer.js:119)
    in AppContainer (at renderApplication.js:39)
reportException @ C:\Users\ezran\Desktop\TestingProjects\testProject\node_modules\react-native\Libraries\Core\ExceptionsManager.js:44
handleException @ C:\Users\ezran\Desktop\TestingProjects\testProject\node_modules\react-native\Libraries\Core\ExceptionsManager.js:113
showErrorDialog @ C:\Users....
.....
.....
....native\Libraries\BatchedBridge\MessageQueue.js:109
(anonymous) @ debuggerWorker.js:80
Show 7 more frames
C:\Users\ezran\Desktop\TestingProjects\testProject\node_modules\react-native\Libraries\Core\ExceptionsManager.js:44

TypeError: Cannot read property 'then' of undefined

This error is located at:
    in App (at renderApplication.js:40)
    in RCTView (at AppContainer.js:101)
    in RCTView (at AppContainer.js:119)
    in AppContainer (at renderApplication.js:39)

Versions:

    "react": "16.9.0",
    "react-native": "0.61.1",
    "react-native-sqlite-storage": "^4.1.0"
Al10s commented 5 years ago

I guess you didn't enable promises with

SQLite.enablePromise(true);

So you can't use db.transaction() as a Promise.

You should still be able catch the callback :

componentDidMount() {
  const { db } = this.state;

  db.transaction(
    (tx) => {
      tx.executeSql('SELECT * FROM tblUser;', [], (tx, results) => {
        const rows = results.rows;
        let users = [];

        for (let i = 0; i < rows.length; i++) {
          users.push({
            ...rows.item(i),
          });
        }

        this.setState({ users });
      })
    },
    (err) => {}),
    () => console.log("TRANSACTION DONE")
  );
}
SnehalAgrawal commented 5 years ago

@Al10s SQLite.enablePromise(true); did the trick.