Syncano Codebox - API call - JSON parsing - Getting a link - Saving new objects

I use Syncano as baas, where I try to call an external API to get a JSON array. This JSON needs to be parsed and then saved in syncano. Before that, I need to get a reference object from the database in order to associate it with a new command object.

I get the command array (json) and the reference object successfully. But I canโ€™t get new data, since only 12-14 teams (should be 18) will be saved.

I tried this with promises, but it didnโ€™t work. Any good advice on how to rewrite code to store all the data? Thank you, that's what I still have ...

//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); }); } }); 
+6
source share
1 answer

Perhaps the problem is that you are completing the request process before all calls are completed, you can 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 concurrent calls at a time is a problem, then chaining one call after another:

  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); }); 
+4
source

All Articles