Delete Value in AsyncStorage React Native
I have a function for looking item exists or not.
addNewPerson(name) {
AsyncStorage.getItem('savedPersons', (err, result) => {
const name = [name];
if (result !== null) {
var newIds = JSON.parse(result).concat(name);
AsyncStorage.setItem('savedPersons', JSON.stringify(newIds));
console.log('Data Found', result);
} else {
AsyncStorage.setItem('savedPersons', JSON.stringify(name));
console.log('Data Added', name);
}
});
}
Now I want to delete some specific person in "savedPersons".
I Tried this code:
AsyncStorage.removeItem('savedPersons','Uzuner');
error text is : "callback is not a function."
How can I delete item in asycnStorage's array?
Solved:
I write this code for deleting item.
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('savedPersons');
let postsFav = JSON.parse(posts);
const postsItems = postsFav.filter(function(e){ return e !== post_id });
// updating 'posts' with the updated 'postsItems'
await AsyncStorage.setItem('savedPersons', JSON.stringify(postsItems));
} catch(error) {
console.log('error: ', error);
}};
Thanks alot all users for replies.
javascript react-native
add a comment |
I have a function for looking item exists or not.
addNewPerson(name) {
AsyncStorage.getItem('savedPersons', (err, result) => {
const name = [name];
if (result !== null) {
var newIds = JSON.parse(result).concat(name);
AsyncStorage.setItem('savedPersons', JSON.stringify(newIds));
console.log('Data Found', result);
} else {
AsyncStorage.setItem('savedPersons', JSON.stringify(name));
console.log('Data Added', name);
}
});
}
Now I want to delete some specific person in "savedPersons".
I Tried this code:
AsyncStorage.removeItem('savedPersons','Uzuner');
error text is : "callback is not a function."
How can I delete item in asycnStorage's array?
Solved:
I write this code for deleting item.
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('savedPersons');
let postsFav = JSON.parse(posts);
const postsItems = postsFav.filter(function(e){ return e !== post_id });
// updating 'posts' with the updated 'postsItems'
await AsyncStorage.setItem('savedPersons', JSON.stringify(postsItems));
} catch(error) {
console.log('error: ', error);
}};
Thanks alot all users for replies.
javascript react-native
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11
add a comment |
I have a function for looking item exists or not.
addNewPerson(name) {
AsyncStorage.getItem('savedPersons', (err, result) => {
const name = [name];
if (result !== null) {
var newIds = JSON.parse(result).concat(name);
AsyncStorage.setItem('savedPersons', JSON.stringify(newIds));
console.log('Data Found', result);
} else {
AsyncStorage.setItem('savedPersons', JSON.stringify(name));
console.log('Data Added', name);
}
});
}
Now I want to delete some specific person in "savedPersons".
I Tried this code:
AsyncStorage.removeItem('savedPersons','Uzuner');
error text is : "callback is not a function."
How can I delete item in asycnStorage's array?
Solved:
I write this code for deleting item.
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('savedPersons');
let postsFav = JSON.parse(posts);
const postsItems = postsFav.filter(function(e){ return e !== post_id });
// updating 'posts' with the updated 'postsItems'
await AsyncStorage.setItem('savedPersons', JSON.stringify(postsItems));
} catch(error) {
console.log('error: ', error);
}};
Thanks alot all users for replies.
javascript react-native
I have a function for looking item exists or not.
addNewPerson(name) {
AsyncStorage.getItem('savedPersons', (err, result) => {
const name = [name];
if (result !== null) {
var newIds = JSON.parse(result).concat(name);
AsyncStorage.setItem('savedPersons', JSON.stringify(newIds));
console.log('Data Found', result);
} else {
AsyncStorage.setItem('savedPersons', JSON.stringify(name));
console.log('Data Added', name);
}
});
}
Now I want to delete some specific person in "savedPersons".
I Tried this code:
AsyncStorage.removeItem('savedPersons','Uzuner');
error text is : "callback is not a function."
How can I delete item in asycnStorage's array?
Solved:
I write this code for deleting item.
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('savedPersons');
let postsFav = JSON.parse(posts);
const postsItems = postsFav.filter(function(e){ return e !== post_id });
// updating 'posts' with the updated 'postsItems'
await AsyncStorage.setItem('savedPersons', JSON.stringify(postsItems));
} catch(error) {
console.log('error: ', error);
}};
Thanks alot all users for replies.
javascript react-native
javascript react-native
edited Dec 31 '18 at 12:37
Hasan Rıza Uzuner
asked Dec 31 '18 at 11:14
Hasan Rıza UzunerHasan Rıza Uzuner
257
257
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11
add a comment |
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11
add a comment |
2 Answers
2
active
oldest
votes
AsyncStorage.removeItem is an asynchronous task which returns a promise or callback. Also, if you want to remove an element from the array then you need to get the array first,delete the element and push it back to the local storage. Something like this,
AsyncStorage.getItem("savedPersons")
.then(persons =>{
const index = persons.indexOf("Uzuner");
const modifiedPersons = persons.splice(index,1);
AsyncStorage.setItem("savedPersons",modifiedPersons)
.then(() => console.log(done))
.catch((error) => console.log("error"));
})
.catch(error => console.log("Error while retrieving the savePersons"));
add a comment |
try this
var savedPersons = JSON.parse(await AsyncStorage.getItem('savedPersons'));
let i = savedPersons.indexOf("Uzuner");
if(i > -1) {
savedPersons.splice(i, 1);
AsyncStorage.setItem('savedPersons', savedPersons);
}
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.
– Chris G
Dec 31 '18 at 12:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986785%2fdelete-value-in-asyncstorage-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
AsyncStorage.removeItem is an asynchronous task which returns a promise or callback. Also, if you want to remove an element from the array then you need to get the array first,delete the element and push it back to the local storage. Something like this,
AsyncStorage.getItem("savedPersons")
.then(persons =>{
const index = persons.indexOf("Uzuner");
const modifiedPersons = persons.splice(index,1);
AsyncStorage.setItem("savedPersons",modifiedPersons)
.then(() => console.log(done))
.catch((error) => console.log("error"));
})
.catch(error => console.log("Error while retrieving the savePersons"));
add a comment |
AsyncStorage.removeItem is an asynchronous task which returns a promise or callback. Also, if you want to remove an element from the array then you need to get the array first,delete the element and push it back to the local storage. Something like this,
AsyncStorage.getItem("savedPersons")
.then(persons =>{
const index = persons.indexOf("Uzuner");
const modifiedPersons = persons.splice(index,1);
AsyncStorage.setItem("savedPersons",modifiedPersons)
.then(() => console.log(done))
.catch((error) => console.log("error"));
})
.catch(error => console.log("Error while retrieving the savePersons"));
add a comment |
AsyncStorage.removeItem is an asynchronous task which returns a promise or callback. Also, if you want to remove an element from the array then you need to get the array first,delete the element and push it back to the local storage. Something like this,
AsyncStorage.getItem("savedPersons")
.then(persons =>{
const index = persons.indexOf("Uzuner");
const modifiedPersons = persons.splice(index,1);
AsyncStorage.setItem("savedPersons",modifiedPersons)
.then(() => console.log(done))
.catch((error) => console.log("error"));
})
.catch(error => console.log("Error while retrieving the savePersons"));
AsyncStorage.removeItem is an asynchronous task which returns a promise or callback. Also, if you want to remove an element from the array then you need to get the array first,delete the element and push it back to the local storage. Something like this,
AsyncStorage.getItem("savedPersons")
.then(persons =>{
const index = persons.indexOf("Uzuner");
const modifiedPersons = persons.splice(index,1);
AsyncStorage.setItem("savedPersons",modifiedPersons)
.then(() => console.log(done))
.catch((error) => console.log("error"));
})
.catch(error => console.log("Error while retrieving the savePersons"));
answered Dec 31 '18 at 12:20
Swarup BamSwarup Bam
716
716
add a comment |
add a comment |
try this
var savedPersons = JSON.parse(await AsyncStorage.getItem('savedPersons'));
let i = savedPersons.indexOf("Uzuner");
if(i > -1) {
savedPersons.splice(i, 1);
AsyncStorage.setItem('savedPersons', savedPersons);
}
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.
– Chris G
Dec 31 '18 at 12:58
add a comment |
try this
var savedPersons = JSON.parse(await AsyncStorage.getItem('savedPersons'));
let i = savedPersons.indexOf("Uzuner");
if(i > -1) {
savedPersons.splice(i, 1);
AsyncStorage.setItem('savedPersons', savedPersons);
}
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.
– Chris G
Dec 31 '18 at 12:58
add a comment |
try this
var savedPersons = JSON.parse(await AsyncStorage.getItem('savedPersons'));
let i = savedPersons.indexOf("Uzuner");
if(i > -1) {
savedPersons.splice(i, 1);
AsyncStorage.setItem('savedPersons', savedPersons);
}
try this
var savedPersons = JSON.parse(await AsyncStorage.getItem('savedPersons'));
let i = savedPersons.indexOf("Uzuner");
if(i > -1) {
savedPersons.splice(i, 1);
AsyncStorage.setItem('savedPersons', savedPersons);
}
edited Dec 31 '18 at 13:36
answered Dec 31 '18 at 11:43
SiSaSiSa
8921720
8921720
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.
– Chris G
Dec 31 '18 at 12:58
add a comment |
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.
– Chris G
Dec 31 '18 at 12:58
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
I don't want to remove savedPersons all values. I want to delete a specific value. this codes is delete all records in savedPersons.
– Hasan Rıza Uzuner
Dec 31 '18 at 12:10
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
@HasanRızaUzuner sorry. I didn't understand correctly. I changed my code
– SiSa
Dec 31 '18 at 12:22
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.– Chris G
Dec 31 '18 at 12:58
AsyncStorage.getItem
is async, the rest of the code needs to go into its callback.– Chris G
Dec 31 '18 at 12:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986785%2fdelete-value-in-asyncstorage-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1. load the string from storage and parse it into array 2. remove person from array 3. save stringified array to storage
– Chris G
Dec 31 '18 at 11:16
thank you. I will try this. Do you have any example code?
– Hasan Rıza Uzuner
Dec 31 '18 at 12:11