dexie/Dexie.js

Filtered Export produces invalid JSON file with an extra comma in data row array

Open

#1,070 创建于 2020年7月5日

在 GitHub 查看
 (4 评论) (0 反应) (0 负责人)JavaScript (10,304 star) (625 fork)batch import
good first issue

描述

Hi,

Today I faced a problem with filtered export json file; Normally I do filtering for many tables without any problems, but this table strangely produces an extra comma in the exported data row array;

Here is the main export function I use for the table;

async function exportMember(memberNo, exportName) {
    if (db === null || memberNo === "") return false;

    memberNo = parseInt(memberNo);

    let rowCount = 0;
    let dbAsBlob = await db.export({
      prettyJson       : true,
      progressCallback : exportProgress,
      noTransaction    : true,
      filter           : (table, value) => {
        let returnValue = table === "member" && value.memberNo === memberNo;
        if (returnValue) rowCount++;
        return returnValue;
      }
    });
    //let jsonDB = JSON.parse(await dbAsBlob.text()); // It has a extra "," in the row list

    //jsonDB.data.tables.forEach(function (table) {
    //  table.rowCount = table.name === "member" ? rowCount : 0;
    //});
    //dbAsBlob = new Blob([JSON.stringify(jsonDB, undefined, 2)], { type : dbAsBlob.type });
    download(dbAsBlob, `${exportName || "ExportedMemberWithNo("}${memberNo}).json`, "application/json");

    return true;
  }

and this func generates a file like this

{
  "formatName": "dexie",
  "formatVersion": 1,
  "data": {
    "databaseName": "MemberDB",
    "databaseVersion": 1,
    "tables": [{ "name": "member", "schema": "++id,&memberNo", "rowCount": 1 }],
    "data": [
      {
        "tableName": "member",
        "inbound": true,
        "rows": [
          {
            "memberNo": 2592,
            "fullName": "Some Name",
            "photo": "data:image/jpeg;base64, very long jpeg data",
            "eMail": "mail@hotmail.com",
            "mobilePhoneNo": "5555555",
            "bloodType": "A Rh(+)",
            "id": 860,
            "birthday": "01.03.1978",
            "someFieldsAgain": "with some data",
            "workPhoneNo": null
          }
          ,
        ]
      }
    ]
  }
}

I use this approach for the other tables with no problem, only difference is the total row count which is 5500, the other tables have less than 100 rows.

贡献者指南