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时报错
184浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
2117浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
238浏览 • 1回复 待解决
HarmonyOS 获取推送token时报错1000900010
188浏览 • 1回复 待解决
拉起UIAbility时报错16000050
2250浏览 • 1回复 待解决
hmscore接入安装HarmonyOS的npm包时报错
809浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
764浏览 • 1回复 待解决
HarmonyOS 运行报错,内存不足
467浏览 • 1回复 待解决
使用featureAbility.getContext()时报错如下
620浏览 • 1回复 待解决
OLED 屏幕输出编译时报错问题
10657浏览 • 2回复 待解决
@ohos/smartrefreshlayout运行报错
331浏览 • 1回复 待解决
HarmonyOS 视频编码surface模式运行报错
316浏览 • 1回复 待解决
HarmonyOS 集成RN 5.0.0.100 后运行报错
351浏览 • 1回复 待解决