从数据库助手类返回数据到组件反应原生 [英] Return data to component from DB helper class react native

查看:19
本文介绍了从数据库助手类返回数据到组件反应原生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

我正在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆