Error : Undefined Is Not An Object (evaluating 'db.transaction') In Reactnative
I am following all the instructions for populating value from existing database for iOS from this :link And Using Following Code: import SQLite from 'react-native-sqlite-storage';
Solution 1:
You can try like this
importSQLitefrom'react-native-sqlite-storage';
var database_name = "sqliteexample"; // Add your Database namevar database_version = "1.0"; // Add your Database Versionvar database_size = 200000; // Add your Database Sizevar database_displayname = "SQL Database"; // Add your Database Displaynamevar db;
exportdefaultclassAppextendsComponent<Props> {
errorCB(err) {
console.log("SQL Error: " + err);
};
successCB() {
console.log("SQL executed fine");
};
openCB() {
console.log("Database OPENED");
};
openDB() {
db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, this.openCB, this.errorCB);
}
populateDB() {
db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, this.openCB, this.errorCB);
db.transaction((tx) => {
tx.executeSql("SELECT * FROM Users", [], (tx, results) => {
console.log("Query completed");
// Get rows with Web SQL Database spec compliance.var len = results.rows.length;
console.log('len' + len)
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
console.log(`User: ${row}`);
}
});
});
}
render() {
return (
<Viewstyle={styles.container} ><ButtononPress={this.openDB}
title="open Database"color="#841584"/><ButtononPress={this.populateDB}
title="Get Data"color="#841584"/></View>
);
}
}
Solution 2:
so I think it could be just timing issue. The safest way to obtain a database object would be inside the success callback which will receive a valid db object if all goes well in openDatabase. so change your code to this and try it...
openCB(myDB) {
console.log("Database OPENED");
db = myDB;
};
openDB() {
SQLite.openDatabase({name : "sqliteexample", createFromLocation : 1}, this.openCB, this.errorCB);
}
Post a Comment for "Error : Undefined Is Not An Object (evaluating 'db.transaction') In Reactnative"