Meteor Collection find()/ fetch()有时会工作

时间:2016-11-16 10:00:25

标签: angularjs mongodb meteor

我开始使用meteor似乎对我的使用有好处,出现了一个问题,我只获得了9/10次文档。我想我实施了一些错误。

我使用Angular 1.5和Typescript

我的收藏集在/

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

以下是来自3个不同页面刷新的console.logs:

enter image description here

enter image description here

enter image description here

看起来我得到的文档数量是完全随机的。

2 个答案:

答案 0 :(得分:1)

以下是一些可以帮助您的代码。它使用" resolve" ui-router的功能是在加载数据之前暂停加载页面。在这种情况下,有两件事正在解决:

  1. 用户记录
  2. 长者记录
  3. 第二个需要一个" elderid"来自users.profile,以便找到一个老年纪录。

    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在数据解析后运行帮助程序。如果你愿意,我也可以为你提供示例代码。

答案 1 :(得分: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);

我现在唯一的问题是,当我创建新位置时,模式会更新像魅力而不是视图。自动运行工作,新位置保存在我的收藏中,但只有在我重新加载时才会看到它。但那个人对我来说不是优先考虑的事。