Tyler Shuster
Tyler Shuster

Reputation: 435

Meteor Method Endlessly Runs

I have this code in a Meteor.methods definition:

update_field: function(collection,document_id,field,value) {
    obj = {};
    obj[field] = value;
    console.log(obj);
    if (collection == 'clients') {
        var Collection = Clients;   
    } else if(collection = 'sites') {
        var Collection = Sites;
    }
    Collection.update(
    {
        _id: document_id
    }, {
        $set: obj
    }, function(error,id) {
        console.log(error,id);
        return(error,id);
    }
    );
}

This method is called from several client-side helpers events, and updates the field as needed. But whenever it runs once, it never stops running. Sometimes it runs infinitely even when all the Meteor.call('update_field')s have been commented out. I have tried including a 'caller' parameter and adding that to all the possible calls to figure out why it keeps getting called to no avail. Any ideas why this is looping?

Edit: this runs 2,000/minute

Edit2: this is called in one of two ways: on a keyup code==13 (enter) in an appropriate field or a field blur. However, event when these calls are commented out, the issue persists.

Upvotes: 0

Views: 67

Answers (2)

Tyler Shuster
Tyler Shuster

Reputation: 435

I think the issue was that one of my methods blurred the input field but preventDefaulted. Then the blur handler was called and caused the loop from there. This is my first Meteor project, so I'm chalking this one up to not quite understanding the system sufficiently. I still find it strange that the method was getting called when the callers were commented out, but I'll figure that one out another day.

Upvotes: 0

Peter Ilfrich
Peter Ilfrich

Reputation: 3816

Especially your second comment worries me:

However, even when these calls are commented out, the issue persists.

Then who is calling it? The behaviour you're describing points to some helper executing the method. The method changes some data, which re-executes the helper (reactivity) and we end up with a classic endless loop.

Check your entire source code for references to this method:

$ grep -r "update_field" *

Maybe you set a variable somehow and then use the variable to call the method. Also: Have you declared the method inside a Meteor.methods({ ... }) block?

Upvotes: 1

Related Questions