diff --git a/interfaces/kits/picker/src/picker_n_exporter.cpp b/interfaces/kits/picker/src/picker_n_exporter.cpp index 767a8f78d68a5ab17e51f6d5dadf9d951745d240..488e63763164cb7dfce750f9b04d5a08d6366a60 100644 --- a/interfaces/kits/picker/src/picker_n_exporter.cpp +++ b/interfaces/kits/picker/src/picker_n_exporter.cpp @@ -140,30 +140,16 @@ static void MakeResultWithBool(napi_env env, std::string key, napi_value &result } } -static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, - std::shared_ptr pickerCallBack) +void GetUriArray(napi_env env, std::vector unifiedDataSet, napi_value &array) { - if (pickerCallBack == nullptr) { - HILOG_ERROR("[picker]: pickerCallBack is nullptr"); - return; - } - napi_value array; - napi_create_array(env, &array); - napi_status status = napi_generic_failure; - if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { - return; - } - const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); - UDMF::QueryOption query = {.key = udkey}; - std::vector unifiedDataSet; - auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); - if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { - HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); - return; - } size_t len = unifiedDataSet[0].GetRecords().size(); + size_t position = 0; for (size_t i = 0; i < len; ++i) { auto readRecord = unifiedDataSet[0].GetRecordAt(i); + if (readRecord == nullptr) { + HILOG_ERROR("[picker]: readRecord is nullptr"); + continue; + } auto entry = readRecord->GetEntry("general.file-uri"); if (!std::holds_alternative>(entry)) { HILOG_ERROR("[picker]: entry is not Object"); @@ -182,11 +168,36 @@ static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value HILOG_ERROR("[picker]: create uri js value fail."); continue; } - status = napi_set_element(env, array, i, uriVal); + auto status = napi_set_element(env, array, position, uriVal); + position++; if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_element failed, error: %{public}d", status); } } +} + +static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, + std::shared_ptr pickerCallBack) +{ + if (pickerCallBack == nullptr) { + HILOG_ERROR("[picker]: pickerCallBack is nullptr"); + return; + } + napi_value array; + napi_create_array(env, &array); + napi_status status = napi_generic_failure; + if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { + return; + } + const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); + UDMF::QueryOption query = {.key = udkey}; + std::vector unifiedDataSet; + auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); + if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { + HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); + return; + } + GetUriArray(env, unifiedDataSet, array); status = napi_set_named_property(env, result, "ability_params_udkey", array); if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_named_property failed");