ReactiveAggregate() + collection.update() ->

反应式聚合最初没有错误地发布到客户端。当来自客户端Meteor.user的 a 更新集合时,错误似乎被触发Meteor.call():


updateProductFavorites = (product_key, action) => {

    const { ranking } = this.props

    const { product_keys } = ranking[0]

    Meteor.call('Accounts.updateProductFavorites', product_key, action, (err, response) => {

        if (err)

            makeAlert(err.reason, 'danger', 3000)

        else 

            this.getProductsByKeys(product_keys)    

    })

}

我已经订阅Meteor.user()了反应聚合和反应聚合:


export default withTracker(() => {

    const handle = Meteor.subscribe("products.RankingList")

    return {

        ranking: AggregatedProductRanking.find({}).fetch(),

        user: Meteor.user(),

        isLoading: !handle.ready() || !Meteor.user()

    }

})(ProductRankingList)

我已经声明并导入了clientCollection双方,正如这个答案中所建议的那样。这是服务器端的相关代码:


const getProductRankingList = (context) => ReactiveAggregate(context, Meteor.users, [

      // aggregation stages can be seen in the code snippet below

    ], { clientCollection: "aggregatedProductRanking"})


Meteor.methods({

    'Accounts.updateProductFavorites': function(product_key, action) {

         allowOrDeny(this.userId)

         action = action == 'add' ? { $addToSet: { productFavorites: product_key }} : { $pull: { productFavorites: product_key }}

         return Meteor.users.update({_id: this.userId}, action)

     }

})


Meteor.publish('products.RankingList', function() {

    const callback = () => this.stop()

    allowOrDenySubscription(this.userId, callback)

    return getProductRankingList(this)

})

让我感到困惑update的Meteor.call('Accounts.updateProductFavorites')是,即使抛出了这个错误,调用者仍然可以可靠地执行。


因此,对登录的更改Meteor.user()流回客户端,组件重新渲染。只有 ReactiveAggregate 的订阅似乎停止工作。抛出以下错误,我必须重新加载浏览器才能看到聚合结果的变化。(底部的完整堆栈跟踪)


Uncaught Error: Expected to find a document to change

    at Object.update (collection.js:207)

    at Object.store.<computed> [as update] (livedata_connection.js:310)

    ...


我猜这update()是由 调用ReactiveAggregate()以填充clientCollection. 但我做错了什么?


猛跑小猪
浏览 133回答 1
1回答

慕尼黑8549860

事实证明,解决方案非常简单:上述错误的原因是聚合结果中缺少_id字段:&nbsp; &nbsp; ...&nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $project: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _id: 0, // This will give the error.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rankingList: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product_keys: "$product_keys.product_keys"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ], { clientCollection: "aggregatedProductRanking"})ReactiveAggregate()( meteor-reactive-aggregate )的文档说明该_id字段可以省略,因为它将由ReactiveAggregate(). 但即使删除了_id: 0,它也不起作用。什么工作是这样的:&nbsp; &nbsp; ...&nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $project: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _id: "whatTheFuckIsGoingOnHere", // Calm down, bro!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rankingList: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; product_keys: "$product_keys.product_keys"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ], { clientCollection: "aggregatedProductRanking"})奇妙的反应性聚合,只是为了在 a** 中带来一点痛苦。我在github repo 中做了一个错误报告
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript