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} `);
  }
}

异常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

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
HarmonyOS
1天前
浏览
收藏 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: '删除失败' })
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
标准化数据通路UDMF传输限制问题
496浏览 • 1回复 待解决
HarmonyOS 怎么使用webview建立数据通
161浏览 • 1回复 待解决
PolarDB 如何进行数据通信?
3083浏览 • 1回复 待解决
HarmonyOS 字符串格式异常
666浏览 • 1回复 待解决
HarmonyOS 数据持久demo
148浏览 • 1回复 待解决
HarmonyOS 手机应用删除数据
540浏览 • 1回复 待解决
mysql怎样批量删除数据
2608浏览 • 1回复 待解决
HarmonyOS 懒加载的数据删除问题
609浏览 • 1回复 待解决
如何删除preferences中缓存的数据
684浏览 • 1回复 待解决