【问题标题】:angular-meteor find MongoDb collection and return based on paramsangular-meteor 根据参数查找 MongoDb 集合并返回
【发布时间】:2015-07-20 08:49:21
【问题描述】:

我正在尝试使用 Meteor 和 Angular.js 的组合在我的 MongoDb 中获取某个地址的警告

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

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

在我的 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();
    }
  }]);
}

我的 mongoDb 集合:

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

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

【问题讨论】:

    标签: angularjs mongodb meteor angular-meteor


    【解决方案1】:

    您应该为此使用$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
    }
    

    【讨论】:

    • 如果我这样做,我不会失去自动发布功能吗? this.warnings = $meteor.collection(Warnings); 会自动将更新推送到数据库,但是当我必须“返回”某些东西时,这不是丢失了吗?
    • 谢谢,我回家后会试试这个,如果可行,我会接受,你会得到你的积分
    • 根据文档: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 我需要所有匹配,而不仅仅是“findOne”功能。
    • @Matt Westlake 如果您需要所有匹配项,请执行相同操作,但使用 $meteor.collection: this.getWarnings = function(findByAddress){ $meteor.collection(Warnings.find({ address: findByAddress } ), 错误的); }
    • @Urigo 我尝试了你所拥有的并得到了TypeError: The first argument of $meteorCollection must be a function or a have a find function property.
    【解决方案2】:

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

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

    旧代码:

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

    新代码:

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

    更详细的bind one tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多