Pumych
Pumych

Reputation: 1398

Update function returns an error

This code returns an "Error: $scope.todoItems.update is not a function" (in markDone function)

As I can see in console, $scope.todoItems has functions like save(), remove() but not an update() function, but as I can see here I writing it right.

Is something that I doing is wrong? More information would be helpful?

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 --------------------------------

This is working:

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 --------------------------------

This is whole code. I do not know if it's a right solution or not but it works.

Is there any example for update exists record in DB?

$ meteor --version

Meteor 1.1.0.2

As I can see in .meteor/versions file:

angular:[email protected]_1

urigo:[email protected]

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>

insert-new-form.ng.html

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

output-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});

        }
    });
}

Upvotes: 0

Views: 108

Answers (1)

Paul Sainsbury
Paul Sainsbury

Reputation: 429

That doesn't look like $scope.todoItems is a Meteor collection. If you look through the documentation you linked (http://docs.meteor.com/#/full/mongo_collection) - there's no reference to Meteor's Mongo Collections having a "save" method.

It looks like you're using an "Angular Meteor Collection" - http://angularjs.meteor.com/api/AngularMeteorCollection

In which case a simple call to ".save" should do it. Perhaps something like this:

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

Upvotes: 1

Related Questions