0

i need to get online status of user here's function where i listen online status

onlineStatus(id) {
 app.database().ref('users').child(id).child('connections').on('value', (snapshot) => {
 return snapshot.val();
 })
 }

but when i try to use this function in return function i get undefined value

{this.state.diologs.map(i =>
 this.state.searchResult.length === 0 && this.state.isDiologOpen === false &&
 <div className="resultField" >
 <img src="/images/noavatar.png" id="userImage" />
 <p className="userName">{i.name} {i.surname} </p>
 <p>{i.lastMessage}</p>
 <button onClick={() => this.writeMessage(i)}>Написать сообщение</button>
 {this.onlineStatus(i.id) ? <p>Online</p> : <p>Offline</p>}

the problem is in this line

{this.onlineStatus(i.id) ? <p>Online</p> : <p>Offline</p>}
asked May 4, 2020 at 15:47
2
  • Use once() instead of on() to query a single time and return a promise to the caller with the data from the query. Commented May 4, 2020 at 15:49
  • It shows undefined because onlineStatus doesn't return any data. The return statement inside onlineStatus currently just return for the callback instead of the method. I don't have enough information about the context of your problem but seems like your approach is not effective and even not doable. React rendering doesn't work that way. Commented May 4, 2020 at 16:04

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.