简体   繁体   English

Meteor 更新 Mongo Sub Collection,未继续

[英]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.但是,我失败得很惨,每次我添加更新代码时,该函数似乎只是在调用该函数,而不会继续执行该函数的其余部分,我可以说在那之后没有任何 console.log 调用被调用.

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.如果在函数中正确设置了 eventID,我可以通过调用访问此对象并将其记录到控制台。

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这是完整的函数,这是使用来自 SyncedCron 重复事件的 Meteor.call 调用的

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.我完全希望我在做一些愚蠢的事情,所以提前抱歉,但我对 Meteor 和 Javascript 都很陌生。

Thanks谢谢

Gareth加雷斯

Unlike normal node.js code, you don't explicitly add a callback to your method signature in meteor.与普通的 node.js 代码不同,您不会在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.有关更多详细信息和示例,请参阅Meteor.call的文档。

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).请注意,如果您从SyncedCron进行调用,那么它正在您的服务器上运行,您可能需要异步调用样式(不要传递回调)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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