代码之家  ›  专栏  ›  技术社区  ›  Lutaaya Huzaifah Idris

firestore部分中的访问方法-vuefire

  •  1
  • Lutaaya Huzaifah Idris  · 技术社区  · 4 年前

    我正在尝试访问的firestore部分中的方法 vuefire ,但我得到了这个错误:

    vue-router.esm.js?8c4f:2257 TypeError: Cannot read property 'messagesWith' of undefined
        at eval (Chat.vue?62f3:214)
    

    这是发生错误的行:

    parents: this.messagesWith(db.auth().currentUser.uid),
    

    以下是我的完整代码:

    export default {
      data: () => ({
    
        activeChat: undefined,
        parents: [],
        messages: [],
        messageForm: {
          content: "",
          me: true,
          createdAt: new Date()
        }
      }),
      firestore: {
        // eslint-disable-next-line no-undef
        messages: db.collection(message_collection_name).orderBy('createdAt','asc'),
        parents: this.messagesWith(db.auth().currentUser.uid),
      },
      computed: {
        
      },
      methods:{
        
        dmCollection(toUid){
          const idPair = [db.auth().currentUser.uid, toUid].join('_').sort();
          return db.firestore().collection(direct_message).doc(idPair).collection(message_collection_name);
        },
        sendDm(toUid, messageText){
          return this.dmCollection(toUid).add({
            from: db.auth().currentUser.uid,
            message: messageText,
            sent: db.firestore.FieldValue.serverTimestamp()
          })
        },
        messagesWith(uid){
          return this.dmCollection(uid).orderBy('sent', 'desc').get();
        },
        
      },
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Boussadjra Brahim    4 年前

    为了访问本地选项/方法,请定义 firestore 作为返回对象的函数:

      firestore:()=> ({
        // eslint-disable-next-line no-undef
        messages: db.collection(message_collection_name).orderBy('createdAt','asc'),
        parents: this.messagesWith(db.auth().currentUser.uid),
      }),