user6215342
user6215342

Reputation:

Meteor mongo get this value

I have a meteor game enter image description here

On the server i have a timer that calls meteor method moveFish.

Meteor.startup(() => {
    Meteor.setInterval(function(){
        Meteor.call("moveFish")
    }, 40);
});

That method selects all fishes are alive and make them move

Meteor.methods({
    moveFish: function(id, speed) {
        Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": 2 } } )
    }
})

enter image description here

How do I move fish using this.fish.speed instead value 2

Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": 2 } } )

*Notice that doesn't work

Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": "fish.speed" } } )

That's works

Meteor.users.find().map( function(user) { x = user.fish.speed Meteor.users.update(user, {$inc: {"fish.positionX": x} }) })

Upvotes: 0

Views: 54

Answers (1)

G07cha
G07cha

Reputation: 4154

Unfortunately document can't use the reference to itself on update operation.

You need to find it first and iterate over all documents manually in this case:

Meteor.users.findAll({ "fish.alive": true }).fetch().forEach(function(fish) {
  fish.positionX += fish.speed;
  fish.save();
});

Upvotes: 0

Related Questions