Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-meteor find MongoDb collection and return based on params

I am trying to get warnings for a certain address in my MongoDb, using a combination of Meteor and Angular.js

In my html file, I'm doing

<div ng-controller = "myController as myCtrl">
{{myCtrl.warnings}}
{{myCtrl.getWarnings("123 Test Street, TestCity, TestState")}}
</div>

in my app.js file:

Warnings = new Mongo.Collection("Warnings");

if (Meteor.isClient) {
  var app = angular.module('ffprototype', [ 'angular-meteor' ]);

  app.controller('myController', ['$window','$meteor', function($window, $meteor) {

    this.warnings = $meteor.collection(Warnings);

    this.getWarnings = function(findByAddress){
        Warnings.find({address: findByAddress}).fetch();
    }
  }]);
}

my mongoDb collection:

{
    "_id": "3ixgxEMZDWGtugxA7",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 1"
}
{
   "_id": "HZH5FvCD5driBYSJz",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 2"
}

The output from the html webpage shows the entire Warnings collection (thanks to {{currentDispatch.warnings}}, but nothing gets displayed for {{currentDispatch.getWarnings("123 Test Street, TestCity, TestState")}}

like image 314
Matt Westlake Avatar asked May 09 '15 13:05

Matt Westlake


1 Answers

You should use $meteor.object for this

this.getWarnings = function(findByAddress){
  $meteor.object(Warnings, { address: findByAddress }, false); // passing false here to not update the collection from changes in the client
}
like image 155
tmaximini Avatar answered Oct 05 '22 01:10

tmaximini