从数据库助手类返回数据到组件反应原生 [英] Return data to component from DB helper class react native
问题描述
我正在使用 sqlite 并且我创建了一个 db helper 类.我没有从组件内部的那个类中获取数据,但是如果我在 db helper 内部进行安慰,它可以正常工作,但在组件中却没有.我给我的代码:-
Cartdb.js.(助手类)
var SQLite = require('react-native-sqlite-storage')db = SQLite.openDatabase('predefine.db', "1.0", "Predefine", -1);类 CartDB {构造函数(){}总项目数 = 0;checkCountOfProduct(){query = "SELECT SUM(count) AS product_count FROM Predefinedcart";db.transaction((tx) => {tx.executeSql(query, [], (tx, results) => {console.log(results.rows.item(0).product_count)this.totalItems = results.rows.item(0).product_count;返回 this.totalItems;},函数(tx,错误){console.log('选择错误:' + error.message);});})}}导出默认的新 CartDB();
<块引用>
组件中的代码:
从'../../../library/CartDB'导入CartDB;类 PredefinedLayout 扩展了 React.Component{构造函数(道具){超级(道具);console.log(CartDB.checkCountOfProduct());}}
我如何在此处获取数据?提前致谢.
它是一个异步操作,这意味着它是一个承诺.最好的方法是将回调传递给函数或将 db 操作作为 promise 和 chain 返回.关于 javascript 中 Promises 的一些文档在这里.
带回调:
class CartDB {构造函数(){}总项目数 = 0;checkCountOfProduct(回调){query = "SELECT SUM(count) AS product_count FROM Predefinedcart";db.transaction((tx) => {tx.executeSql(query, [], (tx, results) => {console.log(results.rows.item(0).product_count)this.totalItems = results.rows.item(0).product_count;回调(this.totalItems)},函数(tx,错误){console.log('选择错误:' + error.message);});})}}
在 Comp 中你调用:CartDB.checkCountOfProduct(count => console.log(count));
承诺:
class CartDB {构造函数(){}总项目数 = 0;checkCountOfProduct(){query = "SELECT SUM(count) AS product_count FROM Predefinedcart";return new Promise((resolve, reject) => db.transaction((tx) => {tx.executeSql(query, [], (tx, results) => {console.log(results.rows.item(0).product_count)this.totalItems = results.rows.item(0).product_count;解决(this.totalItems);},函数(tx,错误){拒绝(错误);});}))}}
在 Comp 中你调用: CartDB.checkCountOfProduct().then(count => console.log(count));
I am using sqlite and I have created a db helper class. I am not getting the data from that class inside component, but if I am consoling inside db helper it is working right, but not in component. I am giving my code:-
Cartdb.js . (Helper class)
var SQLite = require('react-native-sqlite-storage')
db = SQLite.openDatabase('predefine.db', "1.0", "Predefine", -1);
class CartDB {
constructor(){
}
totalItems = 0;
checkCountOfProduct(){
query = "SELECT SUM(count) AS product_count FROM Predefinedcart";
db.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
console.log(results.rows.item(0).product_count)
this.totalItems = results.rows.item(0).product_count;
return this.totalItems;
}, function (tx, error) {
console.log('SELECT error: ' + error.message);
});
})
}
}
export default new CartDB();
Code in Component:
import CartDB from '../../../library/CartDB';
class PredefinedLayout extends React.Component{
constructor(props){
super(props);
console.log(CartDB.checkCountOfProduct());
}
}
How can I get data here? Thanks in advance.
Its an async operation, which means it is a promise. Best way would be to pass a callback to the function or return the db operation as promise and chain then. Some documentation on Promises in javascript is here.
With callback:
class CartDB {
constructor(){
}
totalItems = 0;
checkCountOfProduct(callback){
query = "SELECT SUM(count) AS product_count FROM Predefinedcart";
db.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
console.log(results.rows.item(0).product_count)
this.totalItems = results.rows.item(0).product_count;
callback(this.totalItems)
}, function (tx, error) {
console.log('SELECT error: ' + error.message);
});
})
}
}
and in Comp you call:
CartDB.checkCountOfProduct(count => console.log(count));
With promise:
class CartDB {
constructor(){
}
totalItems = 0;
checkCountOfProduct(){
query = "SELECT SUM(count) AS product_count FROM Predefinedcart";
return new Promise((resolve, reject) => db.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
console.log(results.rows.item(0).product_count)
this.totalItems = results.rows.item(0).product_count;
resolve(this.totalItems);
}, function (tx, error) {
reject(error);
});
}))
}
}
and in Comp you call: CartDB.checkCountOfProduct().then(count => console.log(count));
这篇关于从数据库助手类返回数据到组件反应原生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!