HarmonyOS asset运行add方法时报错

let attr:asset.AssetMap = new Map(); 
attr.set(asset.Tag.SECRET, StringUtils.string2Uint8Array('demo_pwd')); 
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_POWERED_ON); 
attr.set(asset.Tag.IS_PERSISTENT, true); 
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 
try { 
  asset.add(attr).then(() => { 
    log.info(`Asset added successfully.`); 
  }).catch((err: BusinessError) => { 
    log.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
  }) 
} catch (error) { 
  let err = error as BusinessError; 
  log.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
};

第二行开始就报错,报错为:Cannot read property Tag of undefined。

HarmonyOS
4天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

去除attr.set(asset.Tag.IS_PERSISTENT, true);,运行无异常,demo:

import { asset } from '@kit.AssetStoreKit'; 
import { util } from '@kit.ArkTS'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
function stringToArray(str: string): Uint8Array { 
  let textEncoder = new util.TextEncoder(); 
  return textEncoder.encodeInto(str); 
} 
 
function string2Uint8Array(str: string): Uint8Array { 
  if (!str) { 
    console.error('stringToUint8Array', 'params error'); 
    throw new Error('stringToUint8Array, params error'); 
  } 
  let arr: number[] = []; 
  for (let i = 0; i < str.length; ++i) { 
    arr.push(str.charCodeAt(i)); 
  } 
  return new Uint8Array(arr); 
} 
 
export function addAsset() { 
  let attr: asset.AssetMap = new Map(); 
  attr.set(asset.Tag.SECRET, string2Uint8Array('demo_pwd')); 
  attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 
  attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_POWERED_ON); 
  // attr.set(asset.Tag.IS_PERSISTENT, true); 
  attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 
 
  try { 
    asset.remove(attr) 
    asset.add(attr).then(() => { 
      console.info(`Asset added successfully.`); 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
    }) 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
  } 
} 
 
@Entry 
@Component 
struct Index { 
  aboutToAppear(): void { 
    addAsset() 
  } 
 
  build() { 
    Column() { 
      Row() { 
        Text('Hello Huawei') 
      } 
    } 
  } 
}
分享
微博
QQ
微信
回复
4天前
相关问题
import asset from '@ohos.security.asset'报错
1953浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
97浏览 • 1回复 待解决
拉起UIAbility时报错16000050
2086浏览 • 1回复 待解决
hmscore接入安装HarmonyOS的npm包时报错
655浏览 • 1回复 待解决
HarmonyOS 运行报错,内存不足
279浏览 • 1回复 待解决
OLED 屏幕输出编译时报错问题
10463浏览 • 2回复 待解决
使用featureAbility.getContext()时报错如下
454浏览 • 1回复 待解决
HarmonyOS 集成RN 5.0.0.100 后运行报错
258浏览 • 1回复 待解决
HarmonyOS 视频编码surface模式运行报错
225浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
634浏览 • 1回复 待解决
@ohos/smartrefreshlayout运行报错
218浏览 • 1回复 待解决
Please verify and then add 'mergeRule' in the HAP
6205浏览 • 1回复 待解决
HarmonyOS 真机运行报错如何解决
302浏览 • 1回复 待解决
开发自定义弹窗时报错如下
505浏览 • 1回复 待解决