0

这是一个有效的 Apollo 订阅处理程序:

componentDidMount() {
    const fromID = Meteor.userId();
    const {toID} = this.props;
    this.toID = toID;

    this.props.data.subscribeToMore({
        document: IM_SUBSCRIPTION_QUERY,
        variables: {
            fromID: fromID,
            toID: toID,
        },
        updateQuery: (prev, {subscriptionData}) => {
            if (!subscriptionData.data) {
                return prev;
            }
            const newIM = subscriptionData.data.IMAdded;
            // don't double add the message
            if (isDuplicateIM(newIM, prev.instant_message)) {
                return previousResult;
            }
            return update(prev, {
                instant_message: {
                    $push: [newIM],
                },
            });
        }
    });
}

instant_message是要显示的对象数组。每个对象都包含一个日期字段。我需要按日期对这个数组中的对象进行排序。

这种方法曾经适用于 Apollo 的 beta 版本:

//update returns a new "immutable" list
const instant_message = update(previousResult, {instant_message: {$push: [newAppt]}});
instant_message.instant_message = instant_message.instant_message.sort(sortIMsByDateHelper);
return instant_message;

render我可以对数组进行排序,但是 Apollo 会在返回的对象中抛出一个错误——例如,当例程需要它时,它在 props 中找不到。

从返回排序数组的正确方法是什么updateQuery?提前感谢所有人提供的任何信息。

4

1 回答 1

0

事实证明,导致异常的不是排序。__TYPENAME如果返回的对象的 与此处的其他内容不匹配,则订阅似乎失败- 此例程中使用的 varname(上述代码中的“instant_message”),或返回props到渲染中的对象数组的 varname功能。将所有这些东西排列起来,使它们完全相同,可以解决这个问题。

于 2017-07-30T01:25:27.490 回答