How to convert Vue.js realtime chat app from Realtime Database to Firestore

╄→гoц情女王★ 提交于 2020-01-06 06:02:25

问题


I am attempting to build a realtime chatroom app with Vue.js and Firebase, based directly on the following tutorial: https://www.djamware.com/post/5d63586ee8e5fff670f31169/vuejs-tutorial-building-firebase-realtime-chat-web-app. This tutorial uses Firebase Realtime Database for managing chats in dynamically-created chatrooms. My goal is to fully convert the app from Realtime Database to Firestore. I have so far been successful in implementing firebase.get() and firebase.add() methods in the "Room.vue" and "AddRoom.vue" components, in order to add chatrooms to the database and return the chatrooms to the room list in "Room.vue". For "Chat.vue", I am having some issues with reconfiguring the created() life cycle hook to properly set user information in the database and display that user information in a selected chatroom. The current Realtime Database setup of the created() hook in the tutorial is as the following:

  created () {
    let joinData = firebase.database().ref('chatrooms/'+this.roomid+'/chats').push();
    joinData.set({
      type: 'join',
      user: this.nickname,
      message: this.nickname+' has joined this room.',
      sendDate: Date()
    });
    this.data.message = '';
    firebase.database().ref('chatrooms/'+this.roomid+'/chats').on('value', (snapshot) => {
      this.chats = [];
      snapshot.forEach((doc) => {
        let item = doc.val()
        item.key = doc.key
        this.chats.push(item)
      });
    });
  },

This hook enables the creation of a sub-collection inside chatrooms called chats, which receives the roomid generated in Room.vue and ultimately, returns user chat information to the template. I want to achieve something similar to this with Firestore, instead of Realtime database. So far, I have attempted to reconfigure the Chat.vue component as the following:

<script>

import { firebase } from '../Firebase'
import router from '../router'

export default {
  name: 'Chat',
  data () {
    return {
        roomid: this.$route.params.roomid,
        roomname: this.$route.params.roomname,
        nickname: this.$route.params.nickname,
        data: { type:'', nickname:'', message:'' },
        chats: [],
        errors: [],
        offStatus: false
    }
  },
  async created () {
    let joinData = await firebase.push()
    joinData.set({
      type: 'join',
      user: this.nickname,
      message: this.nickname+' has joined this room.',
      sendDate: Date()
    });
    this.data.message = '';
    let snapshot = await firebase.get()
    const chats = []
    snapshot.forEach(doc => {
      let appData = doc.data()
      appData.key = doc.key
      rooms.push(appData)
    })
      this.chats = chats
  }
  },
  methods: {
    onSubmit (evt) {
        evt.preventDefault()

        let newData = firebase.get()
        newData.add({
            type: 'newmsg',
            user: this.nickname,
            message: this.data.message,
            sendDate: Date()
        });
        this.data.message = '';
    },
    exitChat () {
      let exitData = firebase.get()
      exitData.set({
        type: 'exit',
        user: this.nickname,
        message: this.nickname+' has exited this room.',
        sendDate: Date()
      })

      this.offStatus = true
      router.go(-1)
    }
  }
}
</script>

...so that the life cycle hook and the methods instead push and retrieve data to and from Firestore. I am not sure how to configure the hook and methods to create and interact with a subcollection for chats inside an existing chatroom in Firestore, as was done in the Realtime Database example in the tutorial. Any recommendations how to achieve this with Firestore? Thanks!

来源:https://stackoverflow.com/questions/58249933/how-to-convert-vue-js-realtime-chat-app-from-realtime-database-to-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!