Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Cómo filtrar datos Firebase e ionic

Estoy atascado con Firebase. Agregué firebase^4.8.0 a mi proyecto iónico y estoy implementando un chat en vivo. Envío los datos del mensaje enviado de la siguiente manera.

 send(){ this.datasend = { 'sender': this.sessionData.mobile, 'sendTo': this.data.id, 'message': this.chatt, } this.chatt = ''; firebase.database().ref('chats').push(this.datasend).then(res => { // logic here }); }

Hasta aquí todo bien. Estoy usando el número de móvil como ID y cuando envío los datos, reenvío el mensaje, el número de móvil del remitente y el número de móvil del destinatario. Esto funciona perfectamente.

Pero necesito recuperar estos datos. Solo necesito mostrar mensajes al agente y al destinatario. Necesito saber cómo hacer un filtro para esto.

Estoy iniciando Firebase.

Actualmente estoy atascado en esta parte.

 getmessages() { this.ref.on('value', data => { let tmp = []; date.forEach (date => { tmp.push ({ 'sender': data.val(). sender, 'sendTo': data.val(). sendTo, 'message': data.val(). message, }) }); this.messagesList = tmp; }); }

Solo necesito traer mensajes del respectivo chat. Cualquier sugerencia para un filtro u otra forma de almacenar mensajes que me ayudaría a resolver este problema, por favor.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creé un CHAT_ID usando las identificaciones de usuario de los respectivos usuarios. Supongamos que hay dos usuarios con ID: 20 y 4, así que creé una identificación de chat usando estas ID de usuario después de clasificarlas Math.min(20,4) .

 const getMessages = () => { const CHAT_ID = Math.min(currentUser.id, otherUser.id) const chatref = firebase.database().ref(`/chats/${CHAT_ID.toString()}`); chatref.off('value'); chatref.on('value', chatsSnap => { const chatsArray = snapshotToArrayObject(chatsSnap); setStorage(`chats_${chatRoom.current}`, chatsArray); setChatMessages(chatsArray); scrollToRecentMessage(); markAsRead(); }); }

Y al enviar el mensaje:

 const sendMessage = () => { const messageArray = { to: otherUser.id, from: currentUser.id, message: newMessage, messageSeen: false, timeStamp: new Date() }; const CHAT_ID = Math.min(currentUser.id, otherUser.id) firebase.database().ref(`/chats/${CHAT_ID.toString()}`) .push(messageArray); }

Espero que esto pueda ayudarte

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!