IndexedDB 如何解决'DataCloneError'在Angular应用程序中使用RxDb时将文件放入'IDBObjectStore'?

qxsslcnc  于 9个月前  发布在  IndexedDB
关注(0)|答案(1)|浏览(165)

我目前在使用RxDb进行数据存储的Angular应用程序中遇到了一个问题。我遇到的错误是:

main.4e29907b55c8d8bf.js:1 ERROR Error: Uncaught (in promise): DataCloneError: Failed to execute 'put' on 'IDBObjectStore': function arrayBuffer() { [native code] } could not be cloned.DataCloneError: Failed to execute 'put' on 'IDBObjectStore': function arrayBuffer() { [native code] } could not be cloned.

字符串
此错误似乎与在RxDb中处理文件类型时对“IDBObjectStore”执行的“put”操作有关。导致此问题的特定代码段如下:s

async addNewProductForSyncLater(product: Product): Promise<void> {
    product.key = "23";

    let rxDocument: RxDocument = await this.myDatabase['syncProducts'].insert(product);

    try {
      const attachment = await rxDocument.putAttachment({
        id: product.key,  
        data: product.image,
        type: product.image.type
      });

      console.log("Attachment added:", attachment);
    } catch (error) {
      console.error('Error adding attachment:', error);
    }
  }


这是一个接口

export interface Product {
  key ?: string ;
  name :string ;
  basePrice :number ;
  salePrice :number ;
  quantity :number ;
  image : File;
  imageUrl: string;
  category :Category;
  sellQuantity:number ;
}


注意,当inters方法有效并且image属性保存从输入字段中选择的文件时,

afdcj2ne

afdcj2ne1#

IndexedDB只能存储结构化可克隆的类型,这并不包括所有可能的JS变量。例如,函数不是结构化可克隆的。
所以你试图存储的对象的某些属性不是结构化的可克隆的。如果你不确定是什么,我建议你删除它的内容,直到它工作,然后你可以识别出有问题的属性,并找出它不工作的原因,以及如何处理它。

相关问题