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
2024-11-04 10:14:00
浏览
收藏 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
微信
回复
2024-11-04 16:00:54
相关问题
HarmonyOS 运行HmosWorld时报错
94浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
2034浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
160浏览 • 1回复 待解决
HarmonyOS 获取推送token时报错1000900010
85浏览 • 1回复 待解决
拉起UIAbility时报错16000050
2155浏览 • 1回复 待解决
hmscore接入安装HarmonyOS的npm包时报错
725浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
696浏览 • 1回复 待解决
HarmonyOS 运行报错,内存不足
354浏览 • 1回复 待解决
使用featureAbility.getContext()时报错如下
519浏览 • 1回复 待解决
OLED 屏幕输出编译时报错问题
10549浏览 • 2回复 待解决
HarmonyOS 集成RN 5.0.0.100 后运行报错
294浏览 • 1回复 待解决
HarmonyOS 视频编码surface模式运行报错
261浏览 • 1回复 待解决
@ohos/smartrefreshlayout运行报错
259浏览 • 1回复 待解决