react-native-gifted-chat 0.4.3错误Reference.push

时间:2018-09-27 15:27:53

标签: javascript reactjs firebase react-native react-native-gifted-chat

我有一个错误

  

Refereence.push失败:当我按“发送”时,第一个参数包含未定义的属性“ messages.user.id”。   message.js

我找不到未定义的变量在哪里。

    state = {
        messages: [],
    };

    get user() {
        return {
            name: 'test', //TEST
            _id: Fire.shared.uid,
        };
    }

    render() {
        return ( <
             GiftedChat messages = {
               this.state.messages
             }
             onSend = {
               Fire.shared.send
             }
             user = {
               this.user
             }
             />
        );
    }

    componentDidMount() {
        Fire.shared.on(message =>
            this.setState(previousState => ({
                messages: GiftedChat.append(previousState.messages, message),
            }))
        );
    }
    componentWillUnmount() {
        Fire.shared.off();
    }
}

这就是我的文件firebse的代码:

我使用firebse 5.4.2

  import firebase from "firebase"; // 4.8.1

class Fire {
  constructor() {
    this.init();
    this.observeAuth();
  }

  init = () =>
    firebase.initializeApp({
      apiKey: "AIzaSyDWckV-xSAl0lK4KXKgVcC4mdy8s1buIC4",
      authDomain: "mealprep-1537306522704.firebaseapp.com",
      databaseURL: "https://mealprep-1537306522704.firebaseio.com",
      projectId: "mealprep-1537306522704",
      storageBucket: "mealprep-1537306522704.appspot.com",
      messagingSenderId: "491327145443"
    });

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

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

  get uid() {
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        return user.uid;
      } else {
        alert("PROBLEM!!");
      }
    });
  }

  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 the message to the Backend
  send = messages => {
    for (let i = 0; i < messages.length; i++) {
      const { text, user } = messages[i];
      const message = {
        text,
        user,
        timestamp: this.timestamp
      };
      console.log('1' + JSON.stringify(message));
      this.append(message);
    }
  };

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

  // close the connection to the Backend
  off() {
    this.ref.off();
  }
}

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

0 个答案:

没有答案