diff --git a/en/application-dev/reference/apis/js-apis-data-preferences.md b/en/application-dev/reference/apis/js-apis-data-preferences.md index fb937559c7217c6d0429fb85ea0129d31c733492..a9cfb8f798ed9ff2b8e8385767e1d99c7afd1dc9 100644 --- a/en/application-dev/reference/apis/js-apis-data-preferences.md +++ b/en/application-dev/reference/apis/js-apis-data-preferences.md @@ -55,14 +55,14 @@ let preferences = null; try { data_preferences.getPreferences(context, 'mystore', function (err, val) { if (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); - return; - } - preferences = val; - console.info("Obtained the preferences successfully."); - }) + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); + return; + } + preferences = val; + console.info("Succeeded in getting preferences."); + }) } catch (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -78,14 +78,14 @@ class EntryAbility extends UIAbility { try { data_preferences.getPreferences(this.context, 'mystore', function (err, val) { if (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } preferences = val; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }) } catch (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } } } @@ -126,12 +126,12 @@ try { let promise = data_preferences.getPreferences(context, 'mystore'); promise.then((object) => { preferences = object; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }).catch((err) => { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -148,12 +148,12 @@ class EntryAbility extends UIAbility { let promise = data_preferences.getPreferences(this.context, 'mystore'); promise.then((object) => { preferences = object; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }).catch((err) => { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } } } @@ -195,16 +195,16 @@ let context = featureAbility.getContext(); let preferences = null; try { - data_preferences.getPreferences(context, {name: 'mystore'}, function (err, val) { + data_preferences.getPreferences(context, { name: 'mystore' }, function (err, val) { if (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); - return; - } - preferences = val; - console.info("Obtained the preferences successfully."); - }) + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); + return; + } + preferences = val; + console.info("Succeeded in getting preferences."); + }) } catch (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -219,16 +219,16 @@ let preferences = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try { - data_preferences.getPreferences(this.context, {name: 'mystore', dataGroupId:'myId'}, function (err, val) { + data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, val) { if (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } preferences = val; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }) } catch (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } } } @@ -275,15 +275,15 @@ let context = featureAbility.getContext(); let preferences = null; try { - let promise = data_preferences.getPreferences(context, {name: 'mystore'}); + let promise = data_preferences.getPreferences(context, { name: 'mystore' }); promise.then((object) => { preferences = object; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }).catch((err) => { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -297,15 +297,15 @@ let preferences = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try { - let promise = data_preferences.getPreferences(this.context, {name: 'mystore', dataGroupId:'myId'}); + let promise = data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); promise.then((object) => { preferences = object; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }).catch((err) => { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } } } @@ -350,13 +350,13 @@ let context = featureAbility.getContext(); try { data_preferences.deletePreferences(context, 'mystore', function (err) { if (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Deleted the preferences successfully." ); + console.info("Succeeded in deleting preferences." ); }) } catch (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -370,13 +370,13 @@ class EntryAbility extends UIAbility { try { data_preferences.deletePreferences(this.context, 'mystore', function (err) { if (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Deleted the preferences successfully." ); + console.info("Succeeded in deleting preferences." ); }) } catch (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } } } @@ -425,12 +425,12 @@ let context = featureAbility.getContext(); try { let promise = data_preferences.deletePreferences(context, 'mystore'); promise.then(() => { - console.info("Deleted the preferences successfully."); + console.info("Succeeded in deleting preferences."); }).catch((err) => { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -444,12 +444,12 @@ class EntryAbility extends UIAbility { try{ let promise = data_preferences.deletePreferences(this.context, 'mystore'); promise.then(() => { - console.info("Deleted the preferences successfully."); + console.info("Succeeded in deleting preferences."); }).catch((err) => { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } } } @@ -493,15 +493,15 @@ import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { - data_preferences.deletePreferences(context, {name: 'mystore'}, function (err) { + data_preferences.deletePreferences(context, { name: 'mystore' }, function (err) { if (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Deleted the preferences successfully." ); + console.info("Succeeded in deleting preferences." ); }) } catch (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -513,15 +513,15 @@ import UIAbility from '@ohos.app.ability.UIAbility'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try { - data_preferences.deletePreferences(this.context, {name: 'mystore', dataGroupId:'myId'}, function (err) { + data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { if (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Deleted the preferences successfully." ); + console.info("Succeeded in deleting preferences." ); }) } catch (err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } } } @@ -571,14 +571,14 @@ import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { - let promise = data_preferences.deletePreferences(context, {name: 'mystore'}); + let promise = data_preferences.deletePreferences(context, { name: 'mystore' }); promise.then(() => { - console.info("Deleted the preferences successfully."); + console.info("Succeeded in deleting preferences."); }).catch((err) => { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -590,14 +590,14 @@ import UIAbility from '@ohos.app.ability.UIAbility'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try{ - let promise = data_preferences.deletePreferences(this.context, {name: 'mystore', dataGroupId:'myId'}); + let promise = data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); promise.then(() => { - console.info("Deleted the preferences successfully."); + console.info("Succeeded in deleting preferences."); }).catch((err) => { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to delete the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); } } } @@ -635,13 +635,13 @@ let context = featureAbility.getContext(); try { data_preferences.removePreferencesFromCache(context, 'mystore', function (err) { if (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }) } catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -655,13 +655,13 @@ class EntryAbility extends UIAbility { try { data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { if (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }) } catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } } } @@ -702,13 +702,13 @@ import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { let promise = data_preferences.removePreferencesFromCache(context, 'mystore'); - promise.then(() => { - console.info("Removed the preferences successfully."); + promise.then(() => { + console.info("Succeeded in removing preferences."); }).catch((err) => { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -722,12 +722,12 @@ class EntryAbility extends UIAbility { try { let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore'); promise.then(() => { - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }).catch((err) => { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } } } @@ -763,7 +763,7 @@ let context = featureAbility.getContext(); try { data_preferences.removePreferencesFromCacheSync(context, 'mystore'); } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -777,7 +777,7 @@ class EntryAbility extends UIAbility { try { data_preferences.removePreferencesFromCacheSync(this.context, 'mystore'); } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } } } @@ -821,15 +821,15 @@ FA model: import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { - data_preferences.removePreferencesFromCache(context, {name: 'mystore'}, function (err) { + data_preferences.removePreferencesFromCache(context, { name: 'mystore' }, function (err) { if (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }) } catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -841,15 +841,15 @@ import UIAbility from '@ohos.app.ability.UIAbility'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try { - data_preferences.removePreferencesFromCache(this.context, {name: 'mystore', dataGroupId:'myId'}, function (err) { + data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { if (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }) } catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } } } @@ -898,14 +898,14 @@ FA model: import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { - let promise = data_preferences.removePreferencesFromCache(context, {name: 'mystore'}); - promise.then(() => { - console.info("Removed the preferences successfully."); + let promise = data_preferences.removePreferencesFromCache(context, { name: 'mystore' }); + promise.then(() => { + console.info("Succeeded in removing preferences."); }).catch((err) => { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } ``` @@ -917,14 +917,14 @@ import UIAbility from '@ohos.app.ability.UIAbility'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { try { - let promise = data_preferences.removePreferencesFromCache(this.context, {name: 'mystore', dataGroupId:'myId'}); + let promise = data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }); promise.then(() => { - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }).catch((err) => { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } } } @@ -970,13 +970,13 @@ Obtains the value corresponding to the specified key from the cached **Preferenc try { preferences.get('startup', 'default', function (err, val) { if (err) { - console.info("Failed to obtain value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Obtained the value of 'startup' successfully. val: " + val); }) } catch (err) { - console.info("Failed to obtain value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1010,10 +1010,10 @@ try { promise.then((data) => { console.info("Got the value of 'startup'. Data: " + data); }).catch((err) => { - console.info("Failed to obtain value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to obtain value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1043,9 +1043,9 @@ Synchronously obtains the value corresponding to the specified key from the cach ```js try { let value = preferences.getSync('startup', 'default'); - console.info("Obtained the value of 'startup'. Data: " + value); + console.info("Succeeded in getting value of 'startup'. Data: " + value); } catch(err) { - console.info("Failed to obtain value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1069,7 +1069,7 @@ Obtains all KV pairs from the cached **Preferences** instance. This API uses an try { preferences.getAll(function (err, value) { if (err) { - console.info("Failed to obtain all KV pairs. code =" + err.code + ", message =" + err.message); + console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); return; } let allKeys = Object.keys(value); @@ -1077,7 +1077,7 @@ try { console.info("getAll object = " + JSON.stringify(value)); }) } catch (err) { - console.info("Failed to obtain all KV pairs. code =" + err.code + ", message =" + err.message); + console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); } ``` @@ -1106,10 +1106,10 @@ try { console.info('getAll keys = ' + allKeys); console.info("getAll object = " + JSON.stringify(value)); }).catch((err) => { - console.info("Failed to obtain all KV pairs. code =" + err.code + ", message =" + err.message); + console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); }) } catch (err) { - console.info("Failed to obtain all KV pairs. code =" + err.code + ", message =" + err.message); + console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); } ``` @@ -1136,7 +1136,7 @@ try { console.info('getAll keys = ' + allKeys); console.info("getAll object = " + JSON.stringify(value)); } catch (err) { - console.info("Failed to obtain all KV pairs. code =" + err.code + ", message =" + err.message); + console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); } ``` @@ -1162,13 +1162,13 @@ Writes data to the cached **Preferences** instance. This API uses an asynchronou try { preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Failed to put the value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully put the value of 'startup'."); }) } catch (err) { - console.info("Failed to put the value of 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1202,10 +1202,10 @@ try { promise.then(() => { console.info("Successfully put the value of 'startup'."); }).catch((err) => { - console.info("Failed to put the value of 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message); }) } catch(err) { - console.info("Failed to put the value of 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message); } ``` @@ -1231,7 +1231,7 @@ Synchronously writes data to the cached **Preferences** instance. You can use [f try { preferences.putSync('startup', 'auto'); } catch(err) { - console.info("Failed to put the value of 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message); } ``` @@ -1257,7 +1257,7 @@ Checks whether the cached **Preferences** instance contains the KV pair of the g try { preferences.has('startup', function (err, val) { if (err) { - console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); return; } if (val) { @@ -1267,7 +1267,7 @@ try { } }) } catch (err) { - console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1304,10 +1304,10 @@ try { console.info("The key 'startup' is not contained."); } }).catch((err) => { - console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1343,7 +1343,7 @@ try { console.info("The key 'startup' is not contained."); } } catch(err) { - console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1369,13 +1369,13 @@ Deletes a KV pair from the cached **Preferences** instance based on the specifie try { preferences.delete('startup', function (err) { if (err) { - console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Deleted the key 'startup'."); }) } catch (err) { - console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); + console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); } ``` @@ -1405,13 +1405,13 @@ Deletes a KV pair from the cached **Preferences** instance based on the specifie ```js try { let promise = preferences.delete('startup'); - promise.then(() => { + promise.then(() => { console.info("Deleted the key 'startup'."); }).catch((err) => { - console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); }) } catch(err) { - console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); } ``` @@ -1436,7 +1436,7 @@ Synchronously deletes a KV pair from the cached **Preferences** instance based o try { preferences.deleteSync('startup'); } catch(err) { - console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); + console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); } ``` @@ -1461,13 +1461,13 @@ Flushes the data in the cached **Preferences** instance to the persistent file. try { preferences.flush(function (err) { if (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully flushed data."); }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` @@ -1494,10 +1494,10 @@ try { promise.then(() => { console.info("Successfully flushed data."); }).catch((err) => { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` @@ -1520,15 +1520,15 @@ Clears all data in the cached **Preferences** instance. This API uses an asynchr ```js try { - preferences.clear(function (err) { + preferences.clear(function (err) { if (err) { - console.info("Failed to clear data. code =" + err.code + ", message =" + err.message); + console.error("Failed to clear. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully cleared data."); }) } catch (err) { - console.info("Failed to clear data. code =" + err.code + ", message =" + err.message); + console.error("Failed to clear. code =" + err.code + ", message =" + err.message); } ``` @@ -1552,13 +1552,13 @@ Clears all data in the cached **Preferences** instance. This API uses a promise ```js try { let promise = preferences.clear(); - promise.then(() => { - console.info("Successfully cleared data."); + promise.then(() => { + console.info("Successfully cleared data."); }).catch((err) => { - console.info("Failed to clear data. code =" + err.code + ", message =" + err.message); + console.error("Failed to clear. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.info("Failed to clear data. code =" + err.code + ", message =" + err.message); + console.error("Failed to clear. code =" + err.code + ", message =" + err.message); } ``` @@ -1577,7 +1577,7 @@ Synchronously clears all data in the cached **Preferences** instance. You can us try { preferences.clearSync(); } catch(err) { - console.info("Failed to clear data. code =" + err.code + ", message =" + err.message); + console.error("Failed to clear. code =" + err.code + ", message =" + err.message); } ``` @@ -1601,33 +1601,33 @@ Subscribes to data changes. A callback will be triggered to return the new value ```js try { - data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { - if (err) { - console.info("Failed to obtain the preferences."); - return; - } - let observer = function (key) { - console.info("The key " + key + " changed."); - } - preferences.on('change', observer); - preferences.put('startup', 'manual', function (err) { - if (err) { - console.info("Failed to put the value of 'startup'. Cause: " + err); - return; - } - console.info("Successfully put the value of 'startup'."); - - preferences.flush(function (err) { - if (err) { - console.info("Failed to flush data. Cause: " + err); - return; - } - console.info("Successfully flushed data."); - }) - }) - }) + data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { + if (err) { + console.error("Failed to get preferences."); + return; + } + let observer = function (key) { + console.info("The key " + key + " changed."); + } + preferences.on('change', observer); + preferences.put('startup', 'manual', function (err) { + if (err) { + console.error("Failed to put the value of 'startup'. Cause: " + err); + return; + } + console.info("Successfully put the value of 'startup'."); + + preferences.flush(function (err) { + if (err) { + console.error("Failed to flush. Cause: " + err); + return; + } + console.info("Successfully flushed data."); + }) + }) + }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` @@ -1660,86 +1660,85 @@ For details about the error codes, see [User Preference Error Codes](../errorcod ```js try { - data_preferences.getPreferences(this.context, {name: 'mystore', dataGroupId:'myId'}, function (err, preferences) { - if (err) { - console.info("Failed to obtain the preferences."); - return; - } - let observer = function (key) { - console.info("The key " + key + " changed."); - } - preferences.on('multiProcessChange', observer); - preferences.put('startup', 'manual', function (err) { - if (err) { - console.info("Failed to put the value of 'startup'. Cause: " + err); - return; - } - console.info("Successfully put the value of 'startup'."); - - preferences.flush(function (err) { - if (err) { - console.info("Failed to flush data. Cause: " + err); - return; - } - console.info("Successfully flushed data."); - }) - }) - }) + data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { + if (err) { + console.error("Failed to get preferences."); + return; + } + let observer = function (key) { + console.info("The key " + key + " changed."); + } + preferences.on('multiProcessChange', observer); + preferences.put('startup', 'manual', function (err) { + if (err) { + console.error("Failed to put the value of 'startup'. Cause: " + err); + return; + } + console.info("Successfully put the value of 'startup'."); + + preferences.flush(function (err) { + if (err) { + console.error("Failed to flush. Cause: " + err); + return; + } + console.info("Successfully flushed data."); + }) + }) + }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` **Example 2** ```js -let preferences = null; try { data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { if (err) { - console.info("Failed to obtain the preferences."); + console.error("Failed to get preferences."); return; } preferences = val; let observer = function (key) { console.info("The key " + key + " changed."); try { - data_preferences.removePreferencesFromCache(context, { name: 'mystore' }, function (err) { + data_preferences.removePreferencesFromCache(this.context, { name: 'mystore' }, function (err) { if (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } preferences = null; - console.info("Removed the preferences successfully."); + console.info("Succeeded in removing preferences."); }) } catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } try { - data_preferences.getPreferences(context, { name: 'mystore' }, function (err, val) { + data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { if (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } preferences = val; - console.info("Obtained the preferences successfully."); + console.info("Succeeded in getting preferences."); }) } catch (err) { - console.info("Failed to obtain the preferences. code =" + err.code + ", message =" + err.message); + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } } preferences.on('multiProcessChange', observer); preferences.put('startup', 'manual', function (err) { if (err) { - console.info("Failed to put the value of 'startup'. Cause: " + err); + console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Successfully put the value of 'startup'."); preferences.flush(function (err) { if (err) { - console.info("Failed to flush data. Cause: " + err); + console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); @@ -1747,7 +1746,7 @@ try { }) }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` @@ -1772,7 +1771,7 @@ Unsubscribes from data changes. try { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { if (err) { - console.info("Failed to obtain the preferences."); + console.error("Failed to get preferences."); return; } let observer = function (key) { @@ -1781,14 +1780,14 @@ try { preferences.on('change', observer); preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Failed to put the value of 'startup'. Cause: " + err); + console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Successfully put the value of 'startup'."); preferences.flush(function (err) { if (err) { - console.info("Failed to flush data. Cause: " + err); + console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); @@ -1797,7 +1796,7 @@ try { }) }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` @@ -1820,9 +1819,9 @@ Unsubscribes from inter-process data changes. ```js try { - data_preferences.getPreferences(this.context, {name: 'mystore', dataGroupId:'myId'}, function (err, preferences) { + data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { if (err) { - console.info("Failed to obtain the preferences."); + console.error("Failed to get preferences."); return; } let observer = function (key) { @@ -1831,14 +1830,14 @@ try { preferences.on('multiProcessChange', observer); preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Failed to put the value of 'startup'. Cause: " + err); + console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Successfully put the value of 'startup'."); preferences.flush(function (err) { if (err) { - console.info("Failed to flush data. Cause: " + err); + console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); @@ -1847,7 +1846,7 @@ try { }) }) } catch (err) { - console.info("Failed to flush data. code =" + err.code + ", message =" + err.message); + console.error("Failed to flush. code =" + err.code + ", message =" + err.message); } ``` ## ValueType