HarmonyOS 标准化数据通路删除异常

调用标准数据通路删除方法时,直接返回异常code、message都是空字符串’’。

代码如下:

static deleteShareFile() {
  let options: unifiedDataChannel.Options = {
    intention: unifiedDataChannel.Intention.DATA_HUB,
  }

  try {
    unifiedDataChannel.deleteData(options, (err, data) => {
      //此处返回异常
      if (err === undefined) {
        console.info(`Succeeded in deleting data. size = ${data.length}`);
        for (let i = 0; i < data.length; i++) {
          let records = data[i].getRecords();
          for (let j = 0; j < records.length; j++) {
            if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
              let text = records[j] as unifiedDataChannel.PlainText;
              console.info(`${i + 1}.${text.textContent}`);
            }
          }
        }
      } else {
        console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
      }
    });
  } catch (e) {
    let error: BusinessError = e as BusinessError;
    console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

异常stack:

Cannot get SourceMap info, dump raw stack:
=====================Backtrace========================
#00 pc 00000000005e5498 /system/lib64/platformsdk/libark_jsruntime.so
#01 pc 00000000005e5a0c /system/lib64/platformsdk/libark_jsruntime.so
#02 pc 000000000029c3fc /system/lib64/platformsdk/libark_jsruntime.so
#03 pc 0000000000164204 /system/lib64/platformsdk/libark_jsruntime.so
#04 pc 0000000000163da0 /system/lib64/platformsdk/libark_jsruntime.so
#05 pc 00000000001de0d8 /system/lib64/platformsdk/libark_jsruntime.so
#06 pc 00000000004fbb20 /system/lib64/platformsdk/libark_jsruntime.so
#07 pc 00000000004d3abc /system/lib64/platformsdk/libark_jsruntime.so
#08 pc 000000000004c334 /system/lib64/platformsdk/libace_napi.z.so
#09 pc 000000000005e640 /system/lib64/libudmf_data_napi.z.so
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

topstack:

=====================Backtrace========================
#00 pc 00000000005e5498 /system/lib64/platformsdk/libark_jsruntime.so
#01 pc 00000000005e5a0c /system/lib64/platformsdk/libark_jsruntime.so
#02 pc 000000000029c3fc /system/lib64/platformsdk/libark_jsruntime.so
#03 pc 0000000000164204 /system/lib64/platformsdk/libark_jsruntime.so
#04 pc 0000000000163da0 /system/lib64/platformsdk/libark_jsruntime.so
#05 pc 00000000001de0d8 /system/lib64/platformsdk/libark_jsruntime.so
#06 pc 00000000004fbb20 /system/lib64/platformsdk/libark_jsruntime.so
#07 pc 00000000004d3abc /system/lib64/platformsdk/libark_jsruntime.so
#08 pc 000000000004c334 /system/lib64/platformsdk/libace_napi.z.so
#09 pc 000000000005e640 /system/lib64/libudmf_data_napi.z.so
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
HarmonyOS
2025-01-09 14:31:21
1626浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

报错主要是直接调用删掉方法是没有数据的,抛出了异常,需要先insert数据再进行删除。

示例参考如下:

import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = '删除';

  build() {
    Row() {
      Column({ space: 10 }) {
        Button("insert").onClick(() => {
          this.insert()
        })
        Button("delete").onClick(() => {
          this.delete()
        })

      }.width('100%')

    }.height('100%')
  }

  insert() {
    let plainText = new unifiedDataChannel.PlainText();
    plainText.textContent = 'hello world!';
    let unifiedData = new unifiedDataChannel.UnifiedData(plainText);

    // 指定要插入数据的数据通路枚举类型
    let options: unifiedDataChannel.Options = {
      intention: unifiedDataChannel.Intention.DATA_HUB
    }
    try {
      unifiedDataChannel.insertData(options, unifiedData, (err, key) => {
        if (err === undefined) {
          console.info(`Succeeded in inserting data. key = ${key}`);
        } else {
          console.error(`Failed to insert data. code is ${err.code},message is ${err.message} `);
        }
      });
    } catch (e) {
      let error: BusinessError = e as BusinessError;
      console.error(`Insert data throws an exception. code is ${error.code},message is ${error.message} `);
    }
  }

  delete() {
    // 指定要删除数据的数据通路枚举类型
    let options: unifiedDataChannel.Options = {
      intention: unifiedDataChannel.Intention.DATA_HUB
    };

    try {
      unifiedDataChannel.deleteData(options, (err, data) => {
        if (err === undefined) {
          console.info(`Succeeded in deleting data. size = ${data.length}`);
          for (let i = 0; i < data.length; i++) {
            let records = data[i].getRecords();
            for (let j = 0; j < records.length; j++) {
              if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) {
                let text = records[j] as unifiedDataChannel.PlainText;
                console.info(`${i + 1}.${text.textContent}`);
              }
            }
          }
          promptAction.showToast({ message: '删除成功' })
        } else {
          console.error("message" + JSON.stringify(err, Object.getOwnPropertyNames(err), 2))
          console.error(`Failed to delete data. code is ${err.code},message is ${err.message} `);
          promptAction.showToast({ message: '删除失败' })
        }
      });
    } catch (e) {
      let error: BusinessError = e as BusinessError;
      console.error(`Delete data throws an exception. code is ${error.code},message is ${error.message} `);
      promptAction.showToast({ message: '删除失败' })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
分享
微博
QQ
微信
回复
2025-01-09 16:23:17
相关问题
标准化数据通路UDMF传输限制问题
1311浏览 • 1回复 待解决
HarmonyOS 怎么使用webview建立数据通
950浏览 • 1回复 待解决
PolarDB 如何进行数据通信?
3796浏览 • 1回复 待解决
HarmonyOS 字符串格式异常
1506浏览 • 1回复 待解决
HarmonyOS 手机应用删除数据
1159浏览 • 1回复 待解决
HarmonyOS 懒加载的数据删除问题
1298浏览 • 1回复 待解决
HarmonyOS 数据持久demo
900浏览 • 1回复 待解决