参数更改后,动态订阅无法在客户端上接收数据

问题描述

当前尝试更改现有页面订阅,以便用户可以在客户端上设置的一系列过滤器影响结果

客户端上的代码类似于此模板

创建时

Template.primeentry.onCreated(function () {
    let self = this
    this.selectedCompany = Meteor.user().profile.im3core.selectedusergroup;
    this.subscribe('im3coredocumenttype.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3coredocument.allDocsPayments',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corepaymentmode.allFilteredToModes',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corepartnertype.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corepricelist.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corepartner.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corewarehouse.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corecontact.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3coreaccount.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corevat.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corefiscalretention.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3corecompany.mine');

    this.subscribe('im3coremoneysearch.all',{ "__im3coreOwnership.groups": this.selectedCompany })
    this.subscribe('im3coremoneytransactionManual.all',{ "__im3coreOwnership.groups": this.selectedCompany });
    this.subscribe('im3coreprimeEntryManualModes',{ "__im3coreOwnership.groups": this.selectedCompany })
    this.subscribe('im3.core.primeEntryPrevIoUsTotals',{ "__im3coreOwnership.groups": this.selectedCompany })

    this.showTotalColumns = new ReactiveVar(false);
    this.totalBalance = new ReactiveVar(0);
    this.totalAmount = new ReactiveVar(0);
    this.supplier_clientSelector = new ReactiveVar("-1")
    this.supplier_clientId = new ReactiveVar("-1")

    let newDateYear = new Date().getFullYear() + 1
    this.toDateIso = new ReactiveVar(new Date(newDateYear,1))
    this.fromDateIso = new ReactiveVar(new Date(newDateYear - 1,1))
    this.userId = Meteor.userId()
    this.im3money = im3core.im3money
    this.paymentModes = im3core.im3money.fatturaElettronicaenaledPaymentModes
    this.addrows = new ReactiveVar([{ destinations: 1 }])
    this.accountsList = new ReactiveVar([])
    this.accountTotals = []
    this.accountFromId = new ReactiveVar("-1")
    this.Refresh = new ReactiveVar(true)
    this.transactionsList = new ReactiveVar()
    this.movetoLine = -1
    this.PrefilterLine = []
    this.prevIoUsData = new ReactiveVar({})
    this.limit = new ReactiveVar(20)
    this.skip = new ReactiveVar(0)
    this.alreadyPassed = false;
    this.autorun(() => {
        this.subscribe('im3coremoneytransactions.allForPrimeEntry',{
            '__im3coreOwnership.groups': this.selectedCompany,"date": {
                $gte: new Date(this.fromDateIso.get()),$lte: new Date(this.toDateIso.get())
            }
        },{
            sort: { date: -1 },limit: this.limit.get(),skip: this.skip.get()
        })
    })
    Meteor.call('UpdatePrevIoUsYear',this.selectedCompany,new Date().getFullYear(),this.limit.get(),function (err,result) {
        console.log("runsetup ==>",err || result);
        if (err) {

        } else {
            self.prevIoUsData.set(result)
            let valuesList = GetAccountsRender(self)
            console.log("valuesList",valuesList)
            SetupAccountTotalsWithPrevIoUsYearsRender(valuesList,self);
            self.Refresh.set(false)
        }
    });

    //im3primeentry.accountsList=this.accountsList
})

onRendered

Template.primeentry.onRendered(function () {
    let instance = this
    let dateFrom = instance.fromDateIso.get()
    let yyyy = String(new Date(dateFrom).getFullYear())
    let mm = new Date(dateFrom).getMonth() + 1 > 9 ? String(new Date(dateFrom).getMonth() + 1) : "0" + String(new Date(dateFrom).getMonth() + 1)
    let dd = new Date(dateFrom).getDate() > 9 ? String(new Date(dateFrom).getDate()) : "0" + String(new Date(dateFrom).getDate())
    $("#fromDate").val(yyyy + "-" + mm + "-" + dd)
    let dateto = instance.toDateIso.get()
    let year = String(new Date(dateto).getFullYear())
    let month = new Date(dateto).getMonth() + 1 > 9 ? String(new Date(dateto).getMonth() + 1) : "0" + String(new Date(dateto).getMonth() + 1)
    let day = new Date(dateto).getDate() > 9 ? String(new Date(dateto).getDate()) : "0" + String(new Date(dateto).getDate())
    $("#toDate").val(year + "-" + month + "-" + day)

    instance.autorun(() => {
        let isRefreshing = instance.Refresh.get()

        if (this.subscriptionsReady() && isRefreshing == false && instance.alreadyPassed == false) {
            let valueList = Template.instance().accountsList.get()
            console.log("valuelist onrender",valueList)
            /* let valuesList = GetAccounts()
            console.log("valuesList",valuesList)
            SetupAccountTotalsWithPrevIoUsYears(valuesList); */
            let im3money = instance.im3money
            let company = Template.instance().selectedCompany
            Template.instance().registeraccount = im3core.accountCollection.findOne({ "__im3coreOwnership.groups": company,isRegister: true })
            let transactionFilterDateFrom = new Date(Template.instance().fromDateIso.get())
            let transactionFilterDateto = new Date(Template.instance().toDateIso.get())

            let result = [];
            if (typeof company != "undefined") {
                let transactionsList = GetTransactions(im3money,instance)
                transactionsList.forEach(transaction => {
                
                //code i'm omitting because it's not relevant to the problem

                })
            }
            
            

            instance.alreadypassed = true
        }
    })
})

GetTransactions函数

function GetTransactions(im3money,instance) {
    return im3money.moneytransactionsCollection.find({
         '__im3coreOwnership.groups': instance.selectedCompany,"date": {
                $gte: new Date(instance.fromDateIso.get()),$lte: new Date(instance.toDateIso.get())
            }
        },limit: instance.limit.get(),skip: instance.skip.get()
    }).fetch();
}

现在,该页面具有4个控件(2个按钮和2个日期输入)以及用于更新日期和跳过ReactiveVars的相应事件

事件

    "change #fromDate": function (event,instance) {
        let x = $(event.currentTarget).val()
        let dateSplit = x.split('-')
        let dateValue = new Date(dateSplit[0],+dateSplit[1] - 1,dateSplit[2])
        instance.fromDateIso.set(dateValue)

    },"change #toDate": function (event,dateSplit[2])
        instance.toDateIso.set(dateValue)
    },"click .pageBack": function (event,instance) {
        let current = +instance.skip.get()
        current += 20
        instance.skip.set(current)
    },"click .pageNext": function (event,instance) {
        let current = +instance.skip.get()
        if (current >= 20) {
            current -= 20
            instance.skip.set(current)
        }
    }

没有什么花哨的东西让我期望它会造成问题。

但是...

首次使用认的ReactiveVars设置加载页面时,一切正常。客户端按预期从“ im3coremoneytransactions.allForPrimeEntry”订阅中接收数据。

此后,如果我使用输入控件更改日期值,则一切仍然有效,客户端将获得所选日期范围内包含的新值

一旦我尝试更改跳过值,客户端上的GetTransaction就会返回一个空数组。

我知道服务器的发布正确接收到了更改的参数,因为我在其中发布了一行内容来记录将要发送到客户端的内容,并且一切都在此显示,例如服务器日志显示一个数组正确跳过记录更新数量的交易)

我很确定我在这里忽略了一些非常基本的内容,但是日期切换有效且只有跳过更改这一事实并没有使我陷入循环(我希望这两种类型的过滤器都能起作用或返回为空而不是采取相反的方式),而我无法查明错误

解决方法

您需要将反应性数据源移至如下所示的自动运行中。

...
this.autorun(() => {
       this.selectedCompany = Meteor.user().profile.im3core.selectedusergroup;
        this.subscribe('im3coremoneytransactions.allForPrimeEntry',{
            '__im3coreOwnership.groups': this.selectedCompany,"date": {
                $gte: new Date(this.fromDateIso.get()),$lte: new Date(this.toDateIso.get())
            }
        },{
            sort: { date: -1 },limit: this.limit.get(),skip: this.skip.get()
        })
    })
...
,

好吧,忘了跟进,说我发现了问题,而且我担心这真是愚蠢。

我正在对从发布中获得的结果进行跳过,该结果与我在发布本身中用作限制的值完全相同。意味着最终值显然显示为空