I'm encountering an issue with AsyncStorage while attempting to store user input data in JSON format. Despite my efforts to confirm the correct storage of the data using console.log, it consistently shows undefined. I am now wondering how I can access and print out the stored data to verify its accuracy. Any insights on this would be appreciated! Thanks!
Below is the desired JSON format for storing user inputs:
////JSON FORMAT////
const MyRecipeData = [
{
name: recipeName,
video_cover: selectedVideoCover,
video_url: UploadVideo,
servings: servingSize,
channel_name: channelName,
publish_date: uploadDate,
ingredients: ingredientsInput,
directions: directionsInput,
},
];
////JSON FORMAT////
After clicking the upload button, the following functions are invoked, with an attempt to read the saved data through the getAllinput function. However, I am unsure if I am proceeding correctly:
////------- Save all DATA --------------------////
const SaveAllInput = async () => {
await AsyncStorage.setItem("MyRecipeData", JSON.stringify(MyRecipeData))
.then(() => {
alert("your Recipe " + MyRecipeData.name + " has been saved");
})
.catch(() => {
console.log("error");
});
getAllInput();
};
////------- Save all DATA --------------------////
////------- READING THE DATA THAT UPLOAD PREVIOUSLY-------- /////
const getAllInput = async () => {
try {
const NewRecipeData = await AsyncStorage.getItem("MyRecipeData");
NewRecipeData !== null ? JSON.parse(NewRecipeData) : null;
console.log(NewRecipeData);
return NewRecipeData;
} catch {
console.log(error);
}
};
////------- READING THE DATA THAT UPLOAD PREVIOUSLY-------- /////
When checking the terminal, the console.log(NewRecipeData) displays [{}], indicating a problem with reading the data accurately. Despite trying to use getItem, I still receive either undefined or [{}].