angular-meteor基于params找到MongoDb集合并返回

时间:2021-09-14 17:32:04

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

我试图通过使用Meteor和Angular.js的组合来获取MongoDb中某个地址的警告

In my html file, I'm doing

在我的html文件中,我正在做

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

in my app.js file:

在我的app.js文件中:

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:

我的mongoDb系列:

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

html网页的输出显示整个警告集合(感谢{{currentDispatch.warnings}},但{{currentDispatch.getWarnings(“123 Test Street,TestCity,TestState”)}}没有显示任何内容

2 个解决方案

#1


6  

You should use $meteor.object for this

您应该使用$ meteor.object来实现此目的

this.getWarnings = function(findByAddress){
  $meteor.object(Warnings, { address: findByAddress }, false); // passing false here to not update the collection from changes in the client
}

#2


0  

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

从angular-meteor docs看来,$ meteor.object很快就会被弃用。

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

因为我们可以使用Mongo Collection的findOne函数,所以不再需要$ meteor.object了。

Old code:

旧代码:

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

New Code:

新代码:

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

More detailed bind one tutorial.

更详细的绑定一个教程。

#1


6  

You should use $meteor.object for this

您应该使用$ meteor.object来实现此目的

this.getWarnings = function(findByAddress){
  $meteor.object(Warnings, { address: findByAddress }, false); // passing false here to not update the collection from changes in the client
}

#2


0  

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

从angular-meteor docs看来,$ meteor.object很快就会被弃用。

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

因为我们可以使用Mongo Collection的findOne函数,所以不再需要$ meteor.object了。

Old code:

旧代码:

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

New Code:

新代码:

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

More detailed bind one tutorial.

更详细的绑定一个教程。