Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
Penggunaan json.parse di function error, kenapa ya ?
Saya berhasil memasukkan data user ke asyncstorage, dan juga berhasil ketika getItem lewat function get. tapi error saat masuk function getData di trace sih karena JSON.parse(this.get()). Itu kenapa ya ?
import {AsyncStorage} from 'react-native';
export const userHelper = {
isValidData: function(data) {
// data is a json object with token, name, and photo
if(
typeof data === 'object' &&
typeof data.token !== 'undefined' &&
typeof data.u_name !== 'undefined' &&
typeof data.u_email !== 'undefined' &&
typeof data.u_photo !== 'undefined'
)
return true;
else
return false;
},
get: async function() {
// can be used to check if valid data is stored
try{
var data = await AsyncStorage.getItem('userProvesty');
if (data !== null){
if(data && this.isValidData(JSON.parse(data)))
return data;
console.log("hasil get: " + data);
}
} catch (err){
console.log("parsing userHelper get gagal");
}
},
getData: async function(item){
try{
var data = await JSON.parse(this.get());
if(data !== null){
if(data && typeof(data[item]) !== 'undefined')
return data[item];
console.log(data[item]);
}
}
catch(err){
console.log("error getData")
}
},
isLogin: function(){
return this.getData('token');
},
getToken: function() {
return this.getData('token');
}
}
export default userHelper;
0
Tanggapan
tuliskan errornya mas, biar orang bisa bantu,
pake asyncStorage yg community aja jgn pake yg dari react native
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban