Realm JS react-native 无法从具有多对多关系的 API 的大型 JSON 创建表

问题描述

在我的 React-Native 应用程序中,使用 Realm DB JS,我有这三个类:TableOfProducts、Product、BarCodeProducts。

TableOfProducts 类有一个 Products 列表,Product 类有一个 BarcodeProducts 列表。

我像这样为它们建模:

export default class TableOfProductsSchema {
  static schema = {
    name: 'TabelaOfProducts',primaryKey: 'cdTabelaPreco',properties: {
      cdTabelaPreco: {type: 'int',indexed: true},dsTabelaPreco: 'string',product: 'Product[]',//I already tried like: product: {type: 'list',objectType: 'Product'}
    },};
}

export default class ProductSchema {
  static schema = {
    name: 'Product',primaryKey: 'tableProduct',properties: {
      tableProduct: {type: 'string',cdTableOfProduct: 'int',idProduct: 'int',dsProduct: 'string',barcodeproducts: 'ProdutoCodigosBarra[]',//I already tried like: barcodeproducts: {type: 'list',objectType: 'BarCodeProducts'}
    },};
}

export default class BarCodeProductsSchema {
  static schema = {
    name: 'BarCodeProducts',primaryKey: 'idProductNrItem',properties: {
      idProductNrItem: {type: 'string',nrBarCode: 'int',barCodeItem: 'string',barCodeBox: 'string',qtBox: 'double',},};
}

我有这个代码获取 JSON 并在我的 Realm DB 中创建数据:

requestTableOfPoducts = async (httpClient) => {
        let itens = null;
        await httpClient
          .get(apiConfig.url_table_products,{
            timeout: 999000,})
          .then((response) => {
            itens = response.data;
          })
          .catch((error) => console.log(error));        

console.log(itens); //runs ok and show the large JSON in console so my API is receiving itens..

        return new Promise((resolve) => {
          if (itens) {
            const databaSEOptions = {allSchemas};
            Realm.open(databaSEOptions).then((realm) => {
              realm.write(() => {
                const resolveItens = itens.tableOfProducts.map(async (obj,k) => {
                  return realm.create('TableOfProducts',obj,true);
                });
                Promise.all(resolveItens).then(() => resolve(true));
              });
            });
          } else {
            resolve(true);
          }
        });
      };

这里的大问题是:如果我从 API 收到一个小的 JSON 数据,比如一个 TableOfProducts 和一个小产品列表,只有 2 个产品,以及表 BarCodeProducts 中每个产品的两个条形码,我的代码运行正常并且领域保存所有正确记录(TableOfProducts 有 1 条记录,Product 有 2 条记录,BarCodeProducts 有 4 条记录)但是如果我收到一个大的 JSON (5MB),很多记录,比如 4 TableOfProducts 和几乎 50000 个产品,有更多 20000 个 BarCodeProducts,具有相同的结构,我在控制台中收到错误

WARN     Possible Unhandled Promise Rejection (id: 0):
TypeError: undefined is not an object (evaluating 'itens.tableOfProducts.map')

我已经尝试使用“.. JSON.parse(itens) ..”,但我收到一条消息,说我的 JSON 它已经是一个 JSON 解析。

拜托,拜托,拜托..有人可以帮我吗?有什么问题?还有别的方法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)