我无法从Firebase取回数据

时间:2019-02-13 19:44:50

标签: typescript firebase-realtime-database ionic3 angular5 google-cloud-functions

我正在使用Firebase实时数据库,并且Firebase云功能具有以下警告:您正在使用Node.js v8.10.0,但是Google Cloud Functions仅支持v6.11.5。这是我在 index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.addUserMessages = functions.database.ref(`/messages/{messageId}`)
.onWrite( event => {
    const messageKey = event.data.key;
    const messageValue = event.data.val();

    admin.database()
    .ref(`/user-messages/${messageValue.userFromId}/${messageValue.userToId}`)
    .child(messageKey).set(1);
    admin.database()
    .ref(`/user-messages/${messageValue.userToId}/${messageValue.userFromId}`)
    .child(messageKey).set(1);
})

这是我的聊天服务代码

chat.service.ts

import {forkJoin as observableForkJoin,  Observable } from 'rxjs';
import {take, map, mergeMap} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 
'angularfire2/database';
import { Messages } from '../../models/messages/messages.interface';
import { AuthService } from '../auth/auth.service';
import "rxjs/add/observable/forkJoin";
import "rxjs/add/operator/first";
import "rxjs/add/operator/map";
import "rxjs/add/operator/mergeMap";

getChats (userTwoId: string): Observable<Messages[]> {
let database = this.database;
return this.auth.getAuthenticatedUser().pipe(
  map(auth => {
    return auth.uid;
  }),
  mergeMap(uid => {
    console.log('path to list of keys:')
    console.log(`/messages/${uid}/${userTwoId}/`);
    return database.list(`/messages/${uid}/${userTwoId}/`).snapshotChanges();
  }),
  mergeMap(chats => {
    console.log('Log each path to object:');
    const oChats = chats.map(chat => {
      console.log(`/messages/${chat.payload.key}`);
      return <Observable<Messages>> database.list(`/messages/${chat.payload.key}`)
      .valueChanges().pipe(take(1));
    });

    console.log('Log each Obs subscription:')

    oChats.forEach(a => { a.subscribe(b => { console.log(b) }) });
    console.log(oChats);
    return observableForkJoin(oChats);
  }),
);

}

我在哪里调用此函数在

message.ts

import { Component, OnInit } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ManagerProfile } from '../../models/profile/managerProfile.interface';

import { DataService } from '../../providers/data/data.service';
import { Messages } from '../../models/messages/messages.interface';
import { ChatService } from '../../providers/chat/chat.service';
import { Observable } from 'rxjs';
import firebase from 'firebase';

@IonicPage()
@Component({
  selector: 'page-message',
  templateUrl: 'message.html',
})
export class MessagePage {
  
  selectedProfile:  ManagerProfile;
  messageList:Messages[];
  public messageListRef: firebase.database.Reference = firebase.database().ref().child(`messages`);
  
  userId: string;
  userProfile: any;
  
  messages: Observable<Messages[]>
  
  constructor(public navCtrl: NavController, public navParams: NavParams,
    private data: DataService, private chat: ChatService) {}
 

  ionViewWillLoad() {
    this.selectedProfile = this.navParams.get("profile");
    this.data.getAuthenticatedUserProfile()
      .subscribe((profile:  ManagerProfile) => {
        this.userProfile = profile;
        this.userId = profile.key;
      });
      this.chat.getChats(this.selectedProfile.key).subscribe((messages: Messages[]) => {
        this.messageList = messages;
        console.log(messages);
      });

  
  async sendMessage (content: string) {
    try {
      const message: Messages = {   
        userToId: this.selectedProfile.key,
        userToProfile: {
          fullName: this.selectedProfile.fullName
        },
        userFromProfile: {
          fullName: this.userProfile.fullName
        },
        userFromId: this.userId,
        content: content
      }
      await this.chat.sendChat(message);
    }
    catch (e) {
      console.error(e);
    }
  }
  
}

,我的消息界面是:

messages.interface.ts

export interface Messages {

    userFromId: string;

    userFromProfile: {
        fullName: string;
    }

    userToId: string;

    userToProfile: {
        fullName: string;
    }
    content: string;
}

0 个答案:

没有答案