2016-11-16 58 views
0

我开始使用流星,这似乎对我的使用很好,发生问题的地方只有我的文档只有9/10次。我认为我实施了错误的东西。流星收集find()/ fetch()有时候工作

我用角1.5和打字稿

我收藏获取lib文件夹在/

import { Mongo } from 'meteor/mongo'; 

export const Locations= new Mongo.Collection('locations'); 

然后创建的集合数据导入到我的服务

import {app} from '../../js/lib/app'; 
import {Locations} from '../../../lib/collections'; 

export class LocationsService { 

    locations: any; 

    constructor(){ 
     console.log('Constructor called'); 
     this.locations = Locations 
       .find({}) 
       .fetch(); 
     console.log('documents loaded'); 
     console.log(this.locations); 
    } 

    public createLocation(any:any){ 
     Locations.insert(any); 
    } 

    public updateLocation(identity:any, modifier:any){ 
     Locations.update(identity,modifier); 
    } 

} 

app.service('locationsService', LocationsService); 

这里是控制台.logs from 3 different page refresh:

enter image description here

enter image description here

enter image description here

它看起来像文档中我得到的金额完全随机的。

+0

这不是随机的。您需要等待收集准备就绪,您可以在路线中执行此操作(如果您使用的是ui路由器) – Mikkel

+0

您是否可以发布有关它的更多信息? – Opaldes

回答

0

我的新服务只需订阅

export class LocationsService { 

    locations:any; 

    constructor(){ 
     console.log('Constructor called'); 
     //Subscribe to a collection//localStorage.getItem('ID') 
     Meteor.subscribe('locations', 2223); 
     this.locations = Locations; 
     console.log('documents loaded'); 
    } 

    public createLocation(any:any){ 
     Locations.insert(any); 
    } 

    public updateLocation(identity:any, modifier:any){ 
     Locations.update(identity,modifier); 
    } 

} 

app.service('locationsService', LocationsService); 

在我的控制,我只是添加我的文档的抓取的跟踪。

import {app} from '../../js/lib/app'; 
import {LocationsService} from './LocationsService'; 
import {Tracker} from 'meteor/tracker'; 

export class LocationsController { 

    static $inject = ['locationsService','$reactive','$scope']; 

    public $reactive: any; 
    public $scope: any; 

    public locations: any[]; 


    constructor(private locationsService: LocationsService, $reactive:any, $scope:any){ 
     this.locationsService = locationsService; 
     this.$reactive = $reactive; 
     this.$scope = $scope; 
     $reactive(this).attach(this.$scope); 
     Tracker.autorun(() => { 
      //console.log('autorun'); 
      this.locations = locationsService.locations.find({}).fetch(); 
      console.log(this.locations) 
     }); 
    } 

    public createLocation(location:any){ 
     console.log('Locations does what it should'); 
     console.log(location); 
     this.locationsService.createLocation(location); 
    } 

    public updateLocation(location:any, modifier:any){ 
     this.locationsService.updateLocation(location._id,modifier) 
    } 

} 

app.controller('locationsController', LocationsController); 

我现在唯一的问题是,当我创建新的位置时,模型更新像魅力但不是视图。自动运行的作品和新的位置被保存在我的收藏中,但只有在我重新加载时才会看到它。但是那个对我来说并不重要。

1

这里有一些代码可以帮助你。它使用ui-router的“解析”功能来保持页面的加载,直到加载数据。在这种情况下,有两件事情得到解决:

  1. 用户记录
  2. 长老记录

第二个需要从users.profile的“elderid”,以便找到长老纪录。

function config($locationProvider, $urlRouterProvider, $stateProvider) { 
     'ngInject'; 
     $stateProvider 
     .state('member.calendar', { 
      url: '/calendar', 
      template: "<membercalendar></membercalendar>", 
      resolve: { 
      currentUser: ($q) => { 
       var deferred = $q.defer(); 

       Meteor.autorun(function() { 
       if (!Meteor.loggingIn()) { 
        if (Meteor.user() == null) { 
        deferred.reject('AUTH_REQUIRED'); 
        } else { 
         deferred.resolve(Meteor.user()); 
        } 
       } 
       }); 

       return deferred.promise; 
      }, 
      elder: ($q) => { 
       var deferred = $q.defer(); 

       Meteor.autorun(function() { 
       if (!Meteor.loggingIn()) { 
        if (Meteor.user() == null) { 
         deferred.reject('AUTH_REQUIRED'); 
        } else { 
        deferred.resolve(Elders.find({_id: Meteor.user().profile.elderid})); 
        } 
       } 
       }); 

       return deferred.promise; 
      } 
      } 
     }); 

    } 

如果您希望数据在页面加载之前完全加载,这很好。如果您不介意页面的异步更新,则可以使用getReactively在数据解析后运行帮助程序。如果你愿意的话,我也可以为你提供示例代码。

+0

对不起,我想知道getReactivly示例 – Opaldes

+0

如果您觉得它已经解决了您应该完成的问题,请将其标记为正确。这样做的原因是,它可以成为其他可能有类似问题的人知道它适合你的参考。 getReactively部分可以是对此的扩展或另一个问题,我可以在将此标记为正确时回答 – Mikkel

+0

我在我的控制器中使用了Tracker.autorun(),而不是在我的路由中我将发布我的新代码 – Opaldes