2017-02-02 3 views
0

Meteorのクライアント側でサブスクリプションを介して取得されたMongoDBコレクションを使用しようとしています。それに加えて、サーバー側では、公開されたコレクションをリモートサーバーから取得する必要があります。このコードは、標準的な流星の例の1つから適応されました。Meteorはリモートmongoで購読すると空のデータを返します

import { Meteor } from 'meteor/meteor'; 

Meteor.startup(() => { 
    var sconn= new MongoInternals.RemoteCollectionDriver("mongodb://user:[email protected]:27017/mydb"); 
    export const STasks= new Mongo.Collection("myCollection",{_driver: sconn}); 

    Meteor.publish('stasks', function stasksPublication() { 
    return STasks.find({}); 
    }); 
}); 

そして、クライアント上で、私は

import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 

import './main.html'; 

// >>>>> I suppose these two functions are irrelevant for the problem at hand, 
// >>>>> but I left just for sake of completeness. 

Template.hello.onCreated(function helloOnCreated() { 
    // counter starts at 0 
    this.counter = new ReactiveVar(0); 
}); 

Template.hello.helpers({ 
    counter() { 
    return Template.instance().counter.get(); 
    }, 
}); 

// >>>>> Here is where the problem happens: 

Template.hello.events({ 
    'click button'(event, instance) { 
    // increment the counter when button is clicked 
    instance.counter.set(instance.counter.get() + 1); 
    // My code to access data:   
    var STasks=new Mongo.Collection("myCollection"); 
    Meteor.subscribe('stasks'); 
    console.log('Testing data'); 
    console.log(STasks.find().fetch()); 
    }, 
}); 

を持っている。しかし、私はクライアントのコンソールに取得するすべては「[]」が空である:ここでは、サーバーのコードです。私はサーバー上でmyCollection内のドキュメントにアクセスするのに問題はないことを確認しました。

+0

私はリモートデータベースについて何も知らないが、あなたのクリックハンドラでは、subscribe()の直後にfind()をやっている。それは競争状態です。一時停止した後、2回クリックするとデータが表示されますか? – zim

+0

私はこれをしましたが成功しませんでした。 – IRO

答えて

0

はこれを試してみてください:

import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 

import './main.html'; 

var STasks=new Mongo.Collection("myCollection"); 

Template.hello.onCreated(function helloOnCreated() { 
    // ... 
    this.subscribe('stasks'); 
}); 

Template.hello.helpers({ 
    // ... 
}); 

Template.hello.events({ 
    'click button'(event, instance) { 
    // ... 
    console.log(STasks.find().fetch()); 
    }, 
}); 
+0

ありがとうございますが、動作は変わりませんでした。 – IRO

0

私は解決策を見つけた:パブリッシュ側に、find機能のlimitパラメータを使用します。

Meteor.publish('stasks', function stasksPublication() { 
    return STasks.find({},{limit:5}); 
}); 

かなり特異なようだが、私はこれについての説明があります:myCollectionは本当に巨大であり、およそ50万行を含みます。エラーを発生させる代わりに、結果として得られる動作に空の括弧が表示されます。

関連する問題