Read the original article:How to solve the error 15501002 when using dataGroupId in preferences?
Problem Description
In the UIAbility onWindowStageCreate method, the following code was used to initialize a preferences instance:
let options: dataPreferences.Options = {
name: 'byStore', dataGroupId: '01'
};
let preferences = dataPreferences.getPreferencesSync(this.context, options);
After running, the following error occurred:
Error message: The data group id is not valid
Error code: 15501002
SourceCode:
let preferences = dataPreferences.getPreferencesSync(this.context, options);
^
Stacktrace:
at onWindowStageCreate (entry/src/main/ets/entryability/EntryAbility.ets:25:27)
Background Knowledge
- User Preferences provide a key-value storage mechanism to support lightweight persistent data.
- Keys are strings; values can be numbers, strings, booleans, or arrays of these types.
- Error code 15501002 indicates the
dataGroupIdparameter is invalid. -
dataGroupIdmust be obtained through the AppGallery application process. - Constraints:
- Optional parameter; if omitted, preferences are created under the current app’s sandbox.
- Only supported in Stage model.
- Sharing scenarios:
- Between processes of the same app (only for third-party input method apps).
- Across different apps (only for system apps).
Troubleshooting Process
- Checked the error message: “The data group id is not valid” (15501002).
- Verified that the
Optionspassed intogetPreferencescontained a customdataGroupId("01"). - Confirmed that the
dataGroupIdmust be officially obtained via AppGallery, not custom-defined. - Concluded that the invalid parameter caused the runtime failure.
Analysis Conclusion
The root cause of the failure is the use of a custom-defined dataGroupId.
Since dataGroupId values must be applied for and obtained officially from AppGallery, using "01" directly is invalid.
Solution
If the business scenario does not require data sharing via dataGroupId, remove it and only specify the name parameter.
Corrected code example:
let options: dataPreferences.Options = { name: PREFERENCES_NAME };
let preferences = dataPreferences.getPreferencesSync(context, options);
this.tempPreferences = preferences;
Verification Result
After removing the invalid dataGroupId and running the corrected code, the preferences instance was created successfully without triggering error 15501002.
Related Documents or Links
https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-data-preferences
https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-data-preferences#options10
Top comments (0)