简体   繁体   中英

Meteor update Mongo Sub Collection, not proceeding

I have a an array within my object which contains other object and i am trying to selectively update one of those objects based on a value (term) within it. However i am failing miserably, every time i add the update code the function just seems to hand at that call and doesn't proceed through the rest of the function, i can tell as none of the console.log calls after that point get called.

Here is the structure of the object i am fetching:

{ "_id" : "KH5SsND7f9urKSEyz", 
"eventName" : "macmillan", 
"twitterEnabled" : true, 
"instagramEnabled" : false, 
"vineEnabled" : false, 
"fetchCount" : 2, 
"enabled" : true, 
"searchTerms" : [ { "term" : "badger", "latestTwitter" : 0, "latestInstagram" : 0, "latestVine" : 0 }, { "term" : "freedom", "latestTwitter" : 0, "latestInstagram" : 0, "latestVine" : 0 } ] 
}

I can access and log this object to the console by calling if eventID is set properly in the function.

console.log(HTLEvents.find({_id: eventID}));

However when i call this to update the method just stops, no errors, just doesn't proceed.

HTLevents.update( {_id : eventID , "searchTerms.term" : searchTerm } , 
{$set : {"searchTerms.$.latestTwitter" : latest} } , 
false , 
true)

I have triple checked and all the variables used are not undefined and have correct values.

Here is the complete function, this is called using a Meteor.call from a SyncedCron recurring event

Meteor.call('searchTermsUpdateLatest', eventList[eventi]._id, returnValue[i][1].searchedTerm, returnValue[i][1].networkSearched, returnValue[i][1].latestID, function (error, eventList) {function . . . . }

As it is being called with a callback it's running Async.

HTLEvents = new Mongo.Collection('htlevents');

Meteor.methods({
    fetchEnabledEvents: function() {
        return HTLEvents.find({enabled: true}, {}).fetch();
    },
    searchTermsUpdateLatest: function(eventID, searchTerm, network, latest, callback) {
        console.log('Updating:' + eventID);
        console.log(eventID + " " + searchTerm + " " + network + " " + latest);
        console.log(HTLEvents.find({_id: eventID}));
        console.log(HTLEvents.find({_id: eventID , "searchTerms.term" : searchTerm}));
        var err = undefined
        if (eventID && searchTerm && network && latest) {
            console.log("All parameters set, now updating the " + network + " for " + searchTerm);
            if(network === "twitter"){
                console.log("Updating twitter latest id for " + searchTerm);
                HTLevents.update( {_id : eventID , "searchTerms.term" : searchTerm } , 
                {$set : {"searchTerms.$.latestTwitter" : latest} } , 
                false , 
                true)
            } else if (network == "instagram") {
                console.log("Updating instagram latest id for " + searchTerm);
                HTLevents.update({_id : { _str: eventID } , "searchTerms.term":searchTerm} , {$inc: {"searchTerms.$.latestInstagram": latest}});
            } else if (network == "vine") {
                console.log("Updating vine latest id for " + searchTerm);
                HTLevents.update({_id : { _str: eventID } , "searchTerms.term":searchTerm} , {$inc: {"searchTerms.$.latestVine": latest}});
            }
        } else {
            err = "Unable to update latestID, one or more values not set in method call."
            console.log("Unable to update latestID, one or more values not set in method call.")
        }
        console.log('Updated:')
        console.log(HTLevents.find({_id : eventID}));
        callback();
    },
});

I fully expect i'm doing something stupid, so sorry in advance, but i am very new to both Meteor and Javascript.

Thanks

Gareth

Unlike normal node.js code, you don't explicitly add a callback to your method signature in meteor. Your method definition should look like:

searchTermsUpdateLatest: function(eventID, searchTerm, network, latest) {...}

Similarly, you don't invoke the callback at the end of the function (remove the last line). If the method should return a value, you can explicitly do that at the end of your function.

See the documentation for Meteor.call for more details and examples.

Note that if you are making the call from SyncedCron , then it's running on your server and you'll probably want an asynchronous call style (don't pass the callback).

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