I am developing a messenger app and I am trying to do a search of friends by email. So when the user enters another user's email in Prompt text field, typed email compares with those on the server and if a match is found, then it creates a new chat with new friend on server, as well as in the app.
I have one screen called Messages.js and second screen called firebaseChatModule.js which is responsible for all Backend processes. What I need to do: if a match is found, then pass typed email to Backend. I tried to do that with AsyncStorage, but the issue is: every time I receive null instead of email.
How can I fix it and are there any other simple ways how I could do that?
upd:
Okey, now it works, thx to Nirmalsinh for the answer, but now I get following issue on the server:
User1 is the email which I typed in, but there are some strange characters as for example double "" and /
How can I fix this?
You need to use setItem as below:
try {
AsyncStorage.setItem('email', YOUR_VALUE)
} catch (error) {
}
For retrive:
AsyncStorage.getItem('email').then((email) => {
console.log(email)
// YOU CAN KEEP YOUR WHOLE CODE FOR getChatId
}).done();
} catch (error) {
}
Your updated code:
///firebaseChatModule.js
getChatId = () => {
try{
AsyncStorage.getItem('email').then((email) => {
console.log(email)
const IDloc = firebase.database().ref('/rooms');
const newChat = IDloc.push({
title: 'New chat over again'
});
const ChatID = newChat.key;
const membersList = firebase.database().ref('/members').child(ChatID);
const user1 = email
console.log('user1: ', user1);
const user = firebase.auth().currentUser;
membersList.set({
user1: user1,
user2: user.email
});
}).done();
} catch (error) {
}
}
//Messages.js
findUserEmail = (email) => {
firebase.database()
.ref(`/users`)
.orderByChild("email")
.equalTo(email)
.once("value")
.then(snapshot => {
var user = firebase.auth().currentUser;
if (email === user.email) {
Alert.alert("Email is the same as yours!")
} else {
if (snapshot.val()) {
const value = snapshot.val()
this.setState({ email1: email })
const email2 = this.state.email1
AsyncStorage.setItem('email', JSON.stringify(this.state.email1));
console.log('email1: ', this.state.email1);
console.log('email2: ', email2);
FirebaseChatModule.getChatId()
this.setState({ users: Object.keys(value).map((id) => ({
id,
...value[id]
})), promptVisible: false})
} else {
Alert.alert("Email doesn't exist")
}
}
})
}