2016-11-16 61 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