简体   繁体   中英

Meteor: Calling Interdependent External API from Server and dump data to db

I have to call 2 external api's, 2nd one is interdependent on the result of first one, i am not sure whether its better to call it Synch way or async way. I am also dumping the data to db, note that this function is called in the server and runs independent of client. This method runs at regular intervals. Below is my code. Can anybody please suggest me to do this in better way ?

getUser: function(){
        console.log('getMultipleDeviceLocation');
        this.unblock();
        var arrayOfResponse = [];
        try{
            var userData = HTTP.call("GET", "url");
            if(userData && !userData.error){
                var userResult = userData.data;
                var userDateTime = new Date();
                for(key in result){
                    NetworkUsers.insert({
                        'dateTime': userDateTime,
                        'userid':  userResult[key].userid,
                        'userName': userResult[key].userName
                    });

                    try{
                      var response = HTTP.call("GET","url"+result[key].userid);
                      if(response){
                        var result = response.data;
                        var dateTime = new Date();

                            DeviceView.insert({
                                'dateTime' : dateTime,
                                'nearAPs' : result.nearByAPs || '',
                                'userid' : result.userid,
                                'userName': result.userName
                            });
                      }
                    }catch(error){
                      console.log(error);
                    }
                }
            }else{
              console.log('error');
            }
        }catch(error){
          //Main Try Catch Block
          console.log(error);
        }
      }

What i want here is once the first call is done and it returns a response i want those data to be dumped to the db and using the same data i want to make one more all call. Will async create problem here ? do i have to make it sync ?

Well, your actually doing sync HTTP call, which would usually work without any trouble.

I hope you just wrote "url" string to second param because you do not want to show what you're calling on ST. If not, you probably misunderstand something, you have to write the url adress of your api here.

var userData = HTTP.call("GET", "url");

@Julien Leray You mean to say, what i have written will work fine ?

You make me laught on this one; don't you test what your doing?

I dunno the API your trying to reach (like I said before, I hope you're just hidding it!). I don't know if it's work, but the concept should. You don't have anything crazy here, every call are sync, you shouldn't have any trouble.

Now, for going further, you could make it async. I propose you on commentary the Meteor.WrapAsync if you wanted to test pretty sweet Meteor features, but you could also make it vanilla, just by callback.

Eg Meteor.WrapAsync:

//dont want to show up my real Api Adress, using fake one
var url = "http://myApiUrl/foo/bar";
var options = {
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}};

var getToSync = Meteor.wrapAsync(HTTP.get);
var data = getToSync(url, options);
//sweet, you can use data as if it was sync!

Do I well answered to your question or you're missing something?

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