react native giving localstorage getItem not a function error - react-native

I was trying to use the Async Storage official react native API. The code in my react native looks like this:
export function get(key) {
return new Promise(function(resolve, reject) {
AsyncStorage.getItem(key, (err, val) => {
if (err) {
reject(err)
} else {
if (val === null) {
reject('key_not_found')
} else {
var parsedVal = JSON.parse(val)
console.log('got value from storage =>', parsedVal);
resolve(parsedVal)
}
}
})
});
}
I worked for a week without any problem, but recently it starts to give this error
The logger output from remote JS debugger looks like this
I didn't change any of the npm modules except that I installed moment.js using npm install --save moment Any idea what might be happening here?
UPDATE:
The places where I used this exported function are:
//This one is inside the same file so I just use get without module name
export function retrieveCredential() {
return new Promise(function(resolve, reject) {
get('credential')
.then((val) => {
resolve(val)
})
.catch((err) => {
console.log(err)
reject(err)
})
});
}
//this function is also exported
export function autoLogin() {
return new Promise(function(resolve, reject) {
LocalStorage.retrieveCredential()
.then((credential) => {
loginWithEmail (credential.email, credential.password, false)
.then(() => {
resolve()
})
.catch((err) => {
console.log(err)
reject(err)
})
})
.catch((err) => {
console.log(err)
reject(err)
})
});
}
//This is finally used in one of the component
Authentication.autoLogin()
.then(() => { ... })
.catch(() => { ... })
Project search for localstorage.getItem result:

Related

What is the function of this Vue "dispatch"?

I have this existing working VueJs code
const actions = {
retrieveStatus({ rootState, commit, dispatch }) {
return Axios
.get('/abc/GetStatus', {
params: {
draftId: rootState.eform.Id
}
})
.then(response => {
commit('SET_STATUS', response.data.statusCode);
return response.data;
})
.catch(err => {
throw new Error('Errors');
})
},
I don't see anywhere it uses dispatch but it exists there.

use a function in action of auth modules in wizard module in vuex in vue

I have this function in auth.module.js:
async [VERIFY_AUTH](context) {
if (JwtService.getToken()) {
ApiService.setTokenAxios();
return (
ApiService.get("api/customer/me")
.then(({ data }) => {
console.log("auth request - useer:", data);
context.commit(SET_AUTH, data);
})
///////////
.catch(({ response }) => {
console.log(response);
context.commit(SET_ERROR, serviceErrors(response.data));
})
);
} else {
context.commit(PURGE_AUTH);
}
},
I want dispatch it in wizard.modules.js
[SPOUSES](context, data) {
console.log(data);
return new Promise(() => {
ApiService.post(`api/customer/${data.id}/spouses`, data.form).then(
({ data }) => {
console.log(data);
context.dispatch("auth/VERIFY_AUTH", null, { root: true });
}
);
});
},
I try it but it dont work
do you know what should I do?

Why is my VueJS Login reloading despite catch being called?

I have the following code. The catch() is getting called, I can see the toastr alert. However immediately after the page reloads. Any idea why?
signIn() {
this.$store
.dispatch('auth/login', this.credentials)
.then(() => {
this.$toastr.s('You are successfully logged in')
this.$router.push({ name: 'About' })
})
.catch(() => {
// this.$toastr.e('You are successfully logged in')
this.$toastr.e('Please check the form')
})
}
Login Code:
login({ commit }, data) {
commit(types.AUTH_ERROR_CHANGE, null)
// console.log(process.env.VUE_APP_API_URL);
const url = process.env.VUE_APP_API_URL + '/authentication_token'
return new Promise((resolve, reject) => {
axios
.post(url, data)
.then(response => {
commit(types.AUTH_UPDATE_TOKEN, response.data)
resolve(state)
})
.catch(() => {
commit(types.AUTH_ERROR_CHANGE, 'Incorrect username or password')
reject(state)
})
})
},``
Sorry all, the problem was elsewhere completely.
I had an axios interceptor from previous code:
axios.interceptors.response.use(
data => {
store.commit('general/' + types.LOADING_STOP)
return data
},
error => {
store.commit('general/' + types.LOADING_STOP)
if (
error.response &&
error.response.status &&
error.response.status === 401
) {
// window.location.href = '/login'
}
return Promise.reject(error)
}
I had an old location.href there

SecureStore is skipped while I am accessing the login function and returns function error

When I call loginUser function it suppose so access the SecureStore function but instead it is returning the from axios.post method.
Any idea what is going wrong here?
import { SecureStore } from 'expo';
export function loginUser(email, password) {
return function (dispatch) {
return axios.post(SIGNIN_URL, { email, password }).then((response) => {
var { user_id, token } = response.data;
Expo.SecureStore.setItemAsync(user_id, password, options).then(function() {
dispatch(authUser(user_id));
console.log('I am in')
}).catch((error) => {
console.log('Its an error')
dispatch(addAlert("Could not log in."));
});
}).catch((error) => {
dispatch(addAlert("Could not log in."));
});
};
}
The issue is with the way you are constructing your promise chain.
const promiseOne = new Promise((resolve, reject) => {
resolve('p1')
})
const promiseTwo = new Promise((resolve, reject) => {
resolve('p2')
})
promiseOne.then(
(res) => {
console.log('res', res)
return promiseTwo
})
.then((res2) => {
console.log('res2', res2)
})
basically you need to return the call to Expo

Redux saga Firebase callback

I am trying register a firebase callback using redux-saga in react-native but I am not able to figure out how (I am new to redux-saga). Below is the code I want to write in redux saga:
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user logged')
}
});
Wrap your firebase call in a promise like so:
// firebase.js
export function onAuthStateChanged() {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
resolve(user);
} else {
reject(new Error('Ops!'));
}
});
});
}
// in your app
import { onAuthStateChanged } from './firebase';
function * saga() {
try {
const user = yield call(onAuthStateChanged);
// ...
} catch (error) {
console.error(error);
}
}