So, I’m building an internal tool using Meteor in which I need to create some reports using data that’s residing on an external Mongo database (not a Meteor app).
I’m facing a weird timeout issue when trying to query the remote DB (not Meteor).
I’m not using pub/sub, but am trying to query the data inside a method to retrieve the initial dataset and return the data in the method response, so that I can do the necessary filters and transforms on the client. I don’t want reactivity for these queries (I’m worried about the impact of long-polling if the number of connections increase and since the data is static once queried, I can live without reactive updates), which is the main reason I’ve opted to try to use methods.
Based on what I’ve googled, I should be able to do what I need to using MongoInternals.RemoteCollectionDriver
, but the behaviour is very inconsistent, and most often the call timeouts.
I’d like to know what is the right way to non-reactively query an external Mongo instance from Meteor server side and send the query results to the client consistently.
I’m not having any issues querying the same DB from the same dev system using RoboMongo or other Mongo client. It’s only an issue when trying to programatically query from my code.
Here’s what I have in my method definition:
// /server/method.js
import { MongoInternals } from 'meteor/mongo';
import { check } from 'meteor/check';
const dbUrl = 'mongodb://xxxxxxxxx/dbname';
const db = new MongoInternals.RemoteCollectionDriver(dbUrl);
Meteor.methods( {
'getStats' (range, dates) {
if (!this.userId){
throw new Meteor.Error('not-authorized');
}
check(range, Array);
check(dates, Object);
let query = {
data: {$in: range},
datetime: {
$gte: dates.start,
$lte: dates.end
}
};
let res = db.open('collection').find(query).fetch();
console.log(res);
return res;
}
});
I’m calling this from the template (I’m still using ViewModel and Blaze [yes React may be better, but no I’m not looking to move ATM]) as follows:
// client/client.js
getData: function(){
let dates = {};
let self = this;
dates.start = new Date(this.startDate());
dates.end = new Date(this.endDate());
Meteor.call('getStats', range, dates, function(err,res){
if(err)
console.log(err);
else {
console.log(res); // It's enough if I can reliable dump the response in the browser console for now
}
});
}
The getData function is triggered on a button click like so:
// /client/client.html
<button class="button is-medium is-info" {{b "click: getData"}}>Get Data</button>
I’d really appreciate any insights on how to make this work. Thanks in advance.