Meteor Collection不提取数据

时间:2017-01-14 13:34:22

标签: javascript mongodb meteor

我有一个正常工作的跟踪器功能,直到我编辑其他文件并使应用程序更复杂,这导致了这个问题。该函数未返回

的任何数据
  1. vrLoan
  2. fundingData
  3. 我的代码:

    Tracker.autorun(()=>{ // Tracker function for reactivity
      const id = Session.get('data')._id;
      const loans = Loans.find({fileId: id});
    
      loans.forEach(o=>{
        const vrLoan = VRLoans.find({parentId: o._id});
        const fundingData = Funding.find({parentId: o._id});
    
        const t = o.Type;
        const n = o.description;
    
        if(t && n  && vrLoan.count() && fundingData.count()){
          console.log("here")
          fundDep.changed();
          fundingNameSpace[t] = {};
    
          if(t == "Bank Loan" || t == "Directors Loan" || t == "Lease" || t == "Loan Out/Invesment"){
            fundingNameSpace[t][n] = new LoanCalc(t, vrLoan, fundingData);
          }else if(t == "Share"){
            fundingNameSpace[t][n] = new ShareCapCalc(vrLoan, fundingData);
          }else if(t == "Other"){
            fundingNameSpace[t][n] = new OtherIncomeCalc(vrLoan, fundingData);
          }else if(t == "Cap"){
            fundingNameSpace[t][n] = new CapitalGrantCalc(vrLoan, fundingData);
          }
        });
      });
    

    我想我添加的新代码必须以某种方式减慢查询速度,因为查询的长度为0.

    有人可以向我解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您的第一个if语句将无法运行,因为您为vrLoans检查了fundingDatalength,但它们是m​​ongo游标(对象)而不是实际数组。< / p>

因此,您需要.length检查它们是否为空(或在.count()之后调用.fetch()以获取普通的js数组)而不是find()

https://docs.mongodb.com/manual/reference/method/cursor.count/

(但是,由于这个错误,它看起来不像这个函数会做任何事情,因此当你更改应用程序的其余部分时,你可能必须修改它。)