简体   繁体   中英

Meteor client side object update after server method using Angular-Meteor

I have a general question regarding Meteor and it's client side updates in combination with angular.

Let's assume I am using meteors Accounts bundle and using the "users" collection filled with user objects (or any other collection, it doesn't matter). Now I want to build a details page were I want to display the details of one user object.

On this page I am subscribing to the following publication:

// server code
Meteor.publish('userDetails', function(userId) {
  return Meteor.users.find({ _id: userId });
});

On the client side I am subscribing and loading the object like this:

$scope.user = $scope.$meteorObject(Meteor.users, userId, false)
   .subscribe('userDetails', userId);

On the details page is a button which triggers a meteor method which updates the currently displayed user object on the server side. Something like this:

Template:

// client template
<div>User object: {{user}}</div>
<button ng-click="modifyUser()">Do it!</button>

Controller:

// client controller
...
$scope.modifyUser = function() {
    $meteor.call('updateUser', $scope.user._id, 'foo').then(function(result) { 
        ... 
    }, function(err) { 
        ... 
    });
}

Server method:

// server
Meteor.methods({
    updateUser: function(userId, someValue) {
        return Meteor.users.update({ _id: userId }, { $set: { 'profile.someValue': someValue }});
    }
});

Now my question:

What I am expecting is a automatic update of the user object after the updateUser client side method call. But actually nothing is happening. What am I doing wrong?

In general: How do I trigger a client side update of a model which was modified on the server in general? How can this be achieved when using Meteor-Angular?

I found the reason for my problem: All of my Meteor.methods(...) were defined only on the server-side. Now everything seems to work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM