2015-05-03 91 views
0

此代码返回一个“错误:$ scope.todoItems.update不是一个函数” (在markDone功能)更新函数返回一个错误

正如我可以在控制台中看到,$ scope.todoItems具有的功能,如保存(),删除()但不是更新()函数,但正如我所看到的here我写它的权利。

我做的事情是错的吗?更多信息会有帮助吗?

TodoItems = new Mongo.Collection('todoItems'); 

if(Meteor.isClient){ 
    angular.module('todo_am', ['angular-meteor']); 

    angular.module('todo_am').controller('OutputListCtrl', ['$scope', '$meteor', 
     function($scope, $meteor){ 
      $scope.todoItems = $meteor.collection(TodoItems); 

      $scope.remove = function(todoItem){ 
       $scope.todoItems.remove(todoItem); 
      }; 

      $scope.saveCustom = function(todoItem){ 
       todoItem.isRead = false; 
       $scope.todoItems.save(todoItem); 
      }; 

      $scope.markDone = function(todoItem){ 
       console.log('>>> 4', todoItem); 
       console.log('>>> 5', $scope.todoItems); 
       $scope.todoItems.update(todoItem._id, { $set: { isRead: true }}); // <<<<<<<<< This line returns error 
      }; 

      $scope.markUndone = function(todoItem){ 
       todoItem.isRead = true; 
       $scope.todoItems.update(todoItem); 
      }; 
     }]); 
} 

------------------------------ UPDATE ------------ --------------------

这是工作:

if(Meteor.isClient){ 
    angular.module('todo_am', ['angular-meteor']); 

    angular.module('todo_am').controller('OutputListCtrl', ['$scope', '$meteor', 
     function($scope, $meteor){ 
      $scope.todoItems = $meteor.collection(TodoItems); 

      $scope.remove = function(todoItem){ 
       $scope.todoItems.remove(todoItem); 
      }; 

      $scope.saveCustom = function(todoItem){ 
       todoItem.isRead = false; 
       $scope.todoItems.save(todoItem); 
      }; 

      $scope.markDone = function(todoItem){ 
       TodoItems.update({ _id: todoItem._id }, { $set: { isRead: true }}); 
      }; 

      $scope.markUndone = function(todoItem){ 
       TodoItems.update({ _id: todoItem._id }, { $set: { isRead: false }}); 
      }; 
     }]); 
} 

--------------- --------------- UPDATE2 --------------------------------

这是完整的代码。我不知道这是否是一个正确的解决方案,但它的工作原理。

在数据库中有更新存在记录的例子吗?

$流星--version

流星1.1.0.2

正如我在.meteor看到/版本文件:

角:[email protected]_1

urigo:角@ 0.8.6

index.html

<body ng-app="todo_am"> 
<div ng-controller="OutputListCtrl"> 
    <div ng-include="'output-list.ng.html'"></div> 
    <div ng-include="'insert-new-form.ng.html'"></div> 
</div> 
</body> 

index.ng.html

<p>Nothing</p> 

插入 - 新form.ng.html

<div> 
    <input type="text" ng-model="newTodoItem.text" /> 
    <button ng-click="saveCustom(newTodoItem); newTodoItem='';" >Add New</button> 
</div> 

输出list.ng.html

<div> 
    <ul> 
     <li ng-repeat="todoItem in todoItems"> 
      <p> 
       {{ todoItem.text }} 
       <span ng-switch on="todoItem.isRead"> 
        <span ng-switch-when="true"> 
         <button ng-click="markUndone(todoItem)">Mark Undone</button> 
        </span> 
        <span ng-switch-default> 
         <button ng-click="markDone(todoItem)">Mark Done</button> 
        </span> 
       </span> 
       <button ng-click="remove(todoItem)">X</button> 
      </p> 
     </li> 
    </ul> 
</div> 

app.js

TodoItems = new Mongo.Collection('todoItems'); 

if(Meteor.isClient){ 
    angular.module('todo_am', ['angular-meteor']); 

    angular.module('todo_am').controller('OutputListCtrl', ['$scope', '$meteor', 
     function($scope, $meteor){ 
      $scope.todoItems = $meteor.collection(TodoItems); 

      $scope.remove = function(todoItem){ 
       $scope.todoItems.remove(todoItem); 
      }; 

      $scope.saveCustom = function(todoItem){ 
       todoItem.isRead = false; 
       $scope.todoItems.save(todoItem); 
      }; 

      $scope.markDone = function(todoItem){ 
       TodoItems.update({ _id: todoItem._id }, { $set: { isRead: true }}); 
      }; 

      $scope.markUndone = function(todoItem){ 
       TodoItems.update({ _id: todoItem._id }, { $set: { isRead: false }}); 
      }; 
     }]); 
} 

if(Meteor.isServer){ 
    Meteor.startup(function(){ 
     /** 
     * If DB is empty, add some todoItems just for DEV purposes 
     */ 
     if (TodoItems.find().count() === 0) { 

      var todoItems = [ 
       { 
        'text': 'First todo first todo first todo first todo first todo first todo first todo first todo first todo', 
        'isRead': true, 
        'userPosted': 'Vasia' 
       }, 
       { 
        'text': 'Second todo item', 
        'isRead': false, 
        'userPosted': 'Fedia' 
       }, 
       { 
        'text': 'Third todo item', 
        'isRead': true, 
        'userPosted': 'Vasia' 
       } 
      ]; 

      for (var i = 0; i < todoItems.length; i++) 
       TodoItems.insert({text: todoItems[i].text, isRead: todoItems[i].isRead}); 

     } 
    }); 
} 
+0

你能分享完整的代码吗?另外,你使用的是最新版本? – Urigo

+0

在数据库中是否有更新存在记录的例子? – Pumych

回答

1

这看起来并不像$ scope.todoItems是流星集合。如果你浏览一下你链接的文档(http://docs.meteor.com/#/full/mongo_collection) - 没有提到Meteor的Mongo Collections具有“保存”方法。

它看起来像您使用的是“角流星系列” -​​

在这种情况下,一个简单的调用“.save”应该这样做。也许是这样的:

$scope.markDone = function(todoItem){ 
    todoItem.isRead = true; 
    $scope.todoItems.save(todoItem); 
};