angular-meteor find MongoDb collection and return based on params
Asked Answered
M

2

7

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")}}

Motel answered 9/5, 2015 at 13:52 Comment(0)
S
6

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
}
Spontoon answered 11/5, 2015 at 19:35 Comment(5)
If I do this, won't I loose the auto-publish features? the this.warnings = $meteor.collection(Warnings); pushes updates to the database automatically, but when I have to "return" something isn't that lost?Motel
thanks I'll try this out when I get home and if this works I'll accept and you'll get your pointsMotel
according to the documentation: A service that wraps a Meteor object to enable reactivity within AngularJS. Finds the first document that matches the selector, as ordered by sort and skip options. Wraps collection.findOne I need ALL matches not just "findOne" functionality.Motel
@Matt Westlake If you need all matches then do the same but with $meteor.collection: this.getWarnings = function(findByAddress){ $meteor.collection(Warnings.find({ address: findByAddress }), false); }Cow
@Cow I tried what you had and got TypeError: The first argument of $meteorCollection must be a function or a have a find function property.Motel
C
0

From angular-meteor docs, it appears that $meteor.object will soon be deprecated.

There is no need for $meteor.object anymore as we can use Mongo Collection’s findOne function, like so.

Old code:

$scope.party = $meteor.object(Parties, $stateParams.partyId);

New Code:

$scope.helpers({
  party() {
    return Parties.findOne($stateParams.partyId);
  }
});

More detailed bind one tutorial.

Candicandia answered 19/3, 2016 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.