I have a helper that I’m using to populate a datalist element with addresses from a collection. The user could start typing any address at any time, so I can’t really limit a publication of the data intelligently (that I’ve figured out), and provide this “quick look up” action. Basically as the user types more than 3 characters I start querying the address data and trying to provide a 20 row list of options for the user to select from. As the user types more, the list filters more closely to what they may be looking for.
The issue is with 15000 documents of addresses in a publication the UI takes about 10 seconds to become enabled. This is only a small sub-set of the total number of addresses in 1 client database, so the publish and subscribe aren’t going to work I don’t think.
No problem. I created a method on the server only, that takes the typed characters as a parameter, and queries the MongoDB directly. Still pretty quick from what I see coming back in the console logging. So, great.
I’m using this query to search as the user types:
'findMatchAddresses' (partialAddString) {
check(partialAddString, String);
if (!this.userId) {
throw new Meteor.Error('User is not logged in, and not authorized to update user information.');
}
let addressList = Addresses.find({ addressString: {$regex: partialAddString + '.*' }}, { sort: { _id: 1 }, limit: 20 }).fetch();
return addressList;
},
I tried it with just find(), and no fetch(), but got 500 server error trying to return it.
Basically I had
return Addresses.find({ addressString: {$regex: partialAddString + '.*' }}, { sort: { _id: 1 }, limit: 20 });
As I said, got server error, so added the .fetch() and am getting results now.
In my helper, though, how do I get the results to cycle through and display in my datalist element.
Here’s what I have now:
locationInfoData: function() {
let typedIn = Session.get("searchFor");
if (typedIn == "") {
return;
} else {
setTimeout(function() {
Materialize.updateTextFields();
}, 100);
Meteor.call('findMatchAddresses', typedIn, function(err, result) {
if (err) {
console.log('Error finding Address matches: ' + err);
} else {
console.log(result);
return result;
}
});
}
},
I have the timeout just to get the Materialize UI stuff to refresh and show the data, which worked when I was using the publish/subscribe.
Here’s my template:
<input list="locationData" id="callLocation" />
<label for="callLocation">Call Location</label>
<datalist id="locationData">
{{#each locationInfoData}}
<option value="{{this.addressString}}">{{this.landMark}}</option>
{{/each}}
</datalist>
As always, help, links, and hints are greatly appreciated.