简体   繁体   English

资优聊天混乱消息

[英]Gifted Chat disorganized messages

I have a problem with GiftedChat, the messages appear completely disorganized in the app and even looking for messages directly from the firebase (where it is correct), the app does not get a logical order. 我在GiftedChat上遇到问题,消息在应用程序中显得完全混乱,甚至直接从firebase(在正确的位置)查找消息,该应用程序都没有逻辑顺序。 When sending is organized, however the problem is when you load the messages. 发送是有组织的,但是问题是加载消息时。 I'm completely lost 我完全迷路了

loadMessages = async () => {
        const { user } = this.props;
        const matchId = this.props.navigation.getParam('matchId');
        const data = (await firebase.database().ref(`matchs/${matchId}/messages`).limitToLast(300).once('value')).val();

        let messages = [];
        if(data){
          Object.keys(data)
            .forEach(messageId => {
              let message = data[messageId];

              if(_.get(message, 'user._id') !== user.uid) _.push(message);
              messages.push(message);

            });
        }


        this.setState(() => ({
          messages,
        }));

      }

My JSON: 我的JSON:

{
  "-LkAMYoS3fySk46Pbpan" : {
    "_id" : "f5ba3d9a-c346-4f79-b371-c5d54798567e",
    "createdAt" : 1563558815857,
    "text" : "First message",
    "user" : {
      "_id" : "BVY4MDwSaaSDI2bAGjwkZlYktsK2",
      "avatar" : "https://firebasestorage.googleapis.com/v0/b/wefound-760f2.appspot.com/o/users%2FBVY4MDwSaaSDI2bAGjwkZlYktsK2%2Fphotos%2Fk1xuqv26wdrjxoxmp8m.jpg?alt=media&token=7c16a0e4-2cb8-45a5-83a4-635d49c71180",
      "name" : "Rafael"
    }
  },
  "-LkAMZiITDxHE1WfCBGC" : {
    "_id" : "c2755b48-136d-4a68-b283-377ebac7df8e",
    "createdAt" : 1563558819564,
    "text" : "Second message",
    "user" : {
      "_id" : "BVY4MDwSaaSDI2bAGjwkZlYktsK2",
      "avatar" : "https://firebasestorage.googleapis.com/v0/b/wefound-760f2.appspot.com/o/users%2FBVY4MDwSaaSDI2bAGjwkZlYktsK2%2Fphotos%2Fk1xuqv26wdrjxoxmp8m.jpg?alt=media&token=7c16a0e4-2cb8-45a5-83a4-635d49c71180",
      "name" : "Rafael"
    }
  },
  "-LkAM_l4o_w_QeCsYRc8" : {
    "_id" : "65772152-afd9-4353-b752-ac65978a536d",
    "createdAt" : 1563558823838,
    "text" : "Third message",
    "user" : {
      "_id" : "BVY4MDwSaaSDI2bAGjwkZlYktsK2",
      "avatar" : "https://firebasestorage.googleapis.com/v0/b/wefound-760f2.appspot.com/o/users%2FBVY4MDwSaaSDI2bAGjwkZlYktsK2%2Fphotos%2Fk1xuqv26wdrjxoxmp8m.jpg?alt=media&token=7c16a0e4-2cb8-45a5-83a4-635d49c71180",
      "name" : "Rafael"
    }
  },
  "-LkAMcSSTOP7L1CwyiU4" : {
    "_id" : "e69f3a72-0f4e-4c06-a763-518ef1984aa0",
    "createdAt" : 1563558834859,
    "text" : "Fourth message",
    "user" : {
      "_id" : "BVY4MDwSaaSDI2bAGjwkZlYktsK2",
      "avatar" : "https://firebasestorage.googleapis.com/v0/b/wefound-760f2.appspot.com/o/users%2FBVY4MDwSaaSDI2bAGjwkZlYktsK2%2Fphotos%2Fk1xuqv26wdrjxoxmp8m.jpg?alt=media&token=7c16a0e4-2cb8-45a5-83a4-635d49c71180",
      "name" : "Rafael"
    }
  },
  "-LkAMduvBrEnUG6POGKt" : {
    "_id" : "897b2042-25dc-46ec-a5f3-5bdc1fc355dd",
    "createdAt" : 1563558840853,
    "text" : "Fifth message",
    "user" : {
      "_id" : "BVY4MDwSaaSDI2bAGjwkZlYktsK2",
      "avatar" : "https://firebasestorage.googleapis.com/v0/b/wefound-760f2.appspot.com/o/users%2FBVY4MDwSaaSDI2bAGjwkZlYktsK2%2Fphotos%2Fk1xuqv26wdrjxoxmp8m.jpg?alt=media&token=7c16a0e4-2cb8-45a5-83a4-635d49c71180",
      "name" : "Rafael"
    }
  }
}

在此处输入图片说明

I gave console.tron.log () in the messages and they appear disorganized exactly the same is in the app, the problem is in the component? 我在消息中给出了console.tron.log(),它们看起来杂乱无章,完全相同于应用程序,问题出在组件上吗?

1 - refers to the function that loads the messages. 1-指加载消息的功能。

2 - JSON file 2-JSON文件

There are two steps to ordering the data: 排序数据有两个步骤:

  1. Telling the Firebase Database server to return the child nodes in the correct order. 告诉Firebase数据库服务器以正确的顺序返回子节点。
  2. Maintaining that order in your client-side code. 在客户端代码中保持该顺序。

As far as I can tell your code does neither of these, which means the nodes end up in whatever order your client uses for JSON properties (which are by definition unordered). 据我所知,您的代码都没有执行这些操作,这意味着节点以您的客户端用于JSON属性的顺序(按照定义是无序的)结束的。

Let's first see how to retrieve the data in the correct order from Firebase: 首先,让我们看看如何以正确的顺序从Firebase检索数据:

const snapshot = (await firebase.database().ref(`matchs/${matchId}/messages`).orderByChild('createdAt').limitToLast(300).once('value'));

The above orders all child nodes by the value of their createdAt property, then returns the last 300 in order to the client. 上面按其子节点createdAt属性的值对所有子节点进行排序,然后按顺序将最后300个返回给客户端。

You'll note that I don't call val() here yet. 您会注意到,我这里还没有调用val() The reason for that is that snapshot.val() returns a JSON object, and properties in a JSON object have no defined order. 这样做的原因是snapshot.val()返回JSON对象,并且JSON对象中的属性没有定义的顺序。 So if you call .val() too early, you lose the ordering information that the server returned. 因此,如果.val()调用.val() ,则会丢失服务器返回的订购信息。

Next up is processing them in the client-side code to not lose that order, which depends on using DataSnapshot.forEach() : 下一步是在客户端代码中处理它们,以不失去顺序,这取决于使用DataSnapshot.forEach()

data.forEach((message) => {
  messages.push(message.val());
})

Finally, I am able to solve this problem by sorting the JSON which is coming to from the server based on the date and time(CreatedAT). 最后,我可以通过基于日期和时间(CreatedAT)对来自服务器的JSON进行排序来解决此问题。 If the JSON data stored in a variable called discussion then your code should be 如果JSON数据存储在一个称为“讨论”的变量中,则您的代码应为

      discussion.sort(function compare(a, b) {
        var dateA = new Date(a.createdAt);
        var dateB = new Date(b.createdAt);
        return dateB - dateA;
      });  

In your case, data or messages is the one which holds the JSON. 在您的情况下, datamessages是保存JSON的datamessages Add this code once you get the code in JSON format. 一旦获得JSON格式的代码,请添加此代码。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM