2016-11-16 6 views
0

私は流星で作業を始めました。私の使用には良いようですが、私の文書は9/10回しか得られませんでした。私は何かを間違って実装したと思う。Meteor Collection find()/ fetch()working sometimes場合によっては

私が使っ角度1.5と活字体

私のコレクションは、コレクションが

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); 

ここコンソールです私のサービスにインポートされます、その後/

import { Mongo } from 'meteor/mongo'; 

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

でlibフォルダに作成される

.logsは3つの異なるページリフレッシュからのものです:

enter image description here

enter image description here

enter image description here

私が取得ドキュメントの量は完全にランダムであるように見えます。

+0

ランダムではありません。コレクションの準備が整うのを待つ必要があります(ルータで使用している場合) – 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); 

私が今持っている唯一の問題はMODELL魅力のようなアップデートではなく、私は新しい場所を作成するビューということです。自動再生が機能し、新しい場所が自分のコレクションに保存されますが、リロードした場合にのみ表示されます。しかし、それは私の優先順位が低いです。

1

以下は、あなたに役立つコードです。 ui-routerの "解決"機能を使用して、データがロードされるまでページのロードを保留します。この場合、解決されている2つのものがある:

  1. Userレコード
  2. 長老レコード

は、第1には、長老のレコードを見つけるために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

関連する問題