2

我在获取数据信息 firebase 时遇到问题。我正在尝试在我的社交应用程序上创建一个聊天部分,但由于某种原因,我无法弄清楚是什么导致了这个问题。我的密钥在一个单独的 js 文件中,并且在用户身份验证方面工作正常。当我运行 expo 时,我收到以下消息:

Reference.push 失败

这是我的 Fire.js

import FirebaseKeys from "./config";
import firebase from "firebase";
require("firebase/firestore");

class Fire {
    constructor() {
        firebase.initializeApp(FirebaseKeys);
    }

    addPost = async ({ text, localUri }) => {
        const remoteUri = await this.uploadPhotoAsync(localUri);

        return new Promise((res, rej) => {
            this.firestore
                .collection("posts")
                .add({
                    text,
                    uid: this.uid,
                    timestamp: this.timestamp,
                    image: remoteUri
                })
                .then(ref => {
                    res(ref);
                })
                .catch(error => {
                    rej(error);
                });
        });
    };

    uploadPhotoAsync = async uri => {
        const path = `photos/${this.uid}/${Date.now()}.jpg`;

        return new Promise(async (res, rej) => {
            const response = await fetch(uri);
            const file = await response.blob();

            let upload = firebase
                .storage()
                .ref(path)
                .put(file);

            upload.on(
                "state_changed",
                snapshot => {},
                err => {
                    rej(err);
                },
                async () => {
                    const url = await upload.snapshot.ref.getDownloadURL();
                    res(url);
                }
            );
        });
    };


    observeAuth = () =>
    firebase.auth().onAuthStateChanged(this.onAuthStateChanged);

  onAuthStateChanged = user => {
    if (!user) {
      try {
        firebase.auth().signInAnonymously();
      } catch ({ message }) {
        alert(message);
      }
    }
  };

   get uid() {
    return (firebase.auth().currentUser || {}).uid;
  }

  get ref() {
    return firebase.database().ref('messages');
  }

  parse = snapshot => {
    const { timestamp: numberStamp, text, user } = snapshot.val();
    const { key: _id } = snapshot;
    const timestamp = new Date(numberStamp);
    const message = {
      _id,
      timestamp,
      text,
      user,
    };
    return message;
  };

   on = callback =>
    this.ref
      .limitToLast(20)
      .on('child_added', snapshot => callback(this.parse(snapshot)));


  get timestamp() {
    return firebase.database.ServerValue.TIMESTAMP;
  }

    send = messages => {
        for (let i = 0; i < messages.length; i++) {
          const { text, user } = messages[i];
          const message = {
            text,
            user,
            timestamp: this.timestamp,
          };
          this.append(message);
        }
      };

      append = message => this.ref.push(message);


    signOut = () => {
        firebase.auth().signOut();
    };

    get firestore() {
        return firebase.firestore();
    }

    get timestamp() {
        return Date.now();
    }

    off() {
        this.db.off();
    }

}

Fire.shared = new Fire();
export default Fire;
4

1 回答 1

0

在尝试将聊天集成到现有的反应本机应用程序时,我遇到了确切的问题。

我正在使用需要有效user作为道具的 GiftedChat 包装器。

<GiftedChat
    messages={this.state.messages}
    onSend={Fire.send}
    user={this.user}
/>

user对象应由 aname_id组成。错误消息指出_idnull因此,您可以使用当前登录用户的详细信息来填充它。

get user() {
    return {
        _id: firebase.auth().currentUser.uid, // Problem is here. Make sure user is logged in before doing this.
        name: "Prakash",
    };
}

修复后,消息被保存在 Firebase 实时数据库中。

在此处输入图像描述

于 2020-06-26T02:29:42.927 回答