Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncano Codebox - Call API - parse JSON - get Reference - Save new Objects

I am using Syncano as a baas, where I am trying to call an external API to receive a JSON array. This JSON needs to be parsed and afterwards stored in syncano. Before that I need to receive the reference object from the DB to link it to the new team object.

I receive the team (json) array & reference object successfully. But I am unable to get the new data stored, as only 12-14 teams (has to be 18) get saved.

I tried this & that with promises but it didn´t work out. Anyone a good advise how to rewrite the code to store all data? Thank you - here is what I have so far...

//TODO: get from ARGS when executing this codebox
var teamKey = 394;
var requestURL = 'http://api.football-data.org/v1/soccerseasons/' + teamKey + "/teams";

var request = require("request");
var Syncano = require('syncano');
var Promise = require('bluebird');
var account = new Syncano({
  accountKey: "abc"
});
var promises = [];

//from: http://docs.syncano.io/v1.0/docs/data-objects-filtering
//"_eq" means equals to
var filter = {
  "query": {
    "apikey": {
      "_eq": apiKey
    }
  }
};

request({
  headers: {
    'X-Auth-Token': 'abc'
  },
  url: requestURL,
  'Content-Type': 'application/json;charset=utf-8;',
  method: 'GET',
}, function(error, response, body) {
  if (error) {
    console.log(error);
  } else {
    var json = JSON.parse(body);
    var teamArray = json.teams;
    var newObject;

    account.instance('instance').class('competition').dataobject().list(filter)
    .then(function(compRes) {

        var competitionID = compRes.objects[0].id;
        for (var i = 0; i < teamArray.length; i++) {
          newObject = {
            "name": teamArray[i].name,
            "nameshort": teamArray[i].code,
            "logo": teamArray[i].crestUrl,
            "competition": competitionID
          };

          (account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
              console.log(res);
            }).catch(function(err) {
              console.log("Error eq: " + err);
            })
          );
        }
      }).catch(function(err) {
        console.log(err);
      });
  }
});
like image 912
Burkart Avatar asked Feb 22 '16 20:02

Burkart


1 Answers

The issue might be you are finishing the request process before all the save calls are made, you could try Promise.all():

    account.instance('instance').class('competition').dataobject().list(filter)
    .then(function(compRes) {

        var competitionID = compRes.objects[0].id, promises=[];
        for (var i = 0; i < teamArray.length; i++) {
          newObject = {
            "name": teamArray[i].name,
            "nameshort": teamArray[i].code,
            "logo": teamArray[i].crestUrl,
            "competition": competitionID
          };
          promises.push(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) {
              console.log(res);
            }).catch(function(err) {
              console.log("Error eq: " + err);
            })
          );
        }
        return Promise.all(promises);
      }).catch(function(err) {
        console.log(err);
      });

if too many parallel calls at a time is the issue then chain one call after other:

    account.instance('instance').class('competition').dataobject().list(filter)
    .then(function(compRes) {

        var competitionID = compRes.objects[0].id, promise = Promise.resolve();
        function chainToPromise(promise, teamObj, waitTime){
          waitTime = waitTime || 500;
          return promise.then(function(){
            return new Promise(function(resolve, reject){
              setTimeout(resolve, waitTime);
            });
          }).then(function(){
            return account.instance('instance').class('teams').dataobject().add(teamObj);
          }).then(function(res) {
            console.log(res);
          }).catch(function(err) {
            console.log("Error eq: " + err);
          });
        }
        for (var i = 0; i < teamArray.length; i++) {
          newObject = {
            "name": teamArray[i].name,
            "nameshort": teamArray[i].code,
            "logo": teamArray[i].crestUrl,
            "competition": competitionID
          };
           promise = chainToPromise(promise, newObject);
        }
        return promise;
      }).catch(function(err) {
        console.log(err);
      });
like image 60
mido Avatar answered Oct 24 '22 03:10

mido