I'm having trouble downloading json with arrays, it's just inserting the last result, I do not know how it will insert into each loop. Someone can help me follow the code:
My backend:
var xhr = Ti.Network.createHTTPClient();
var signos;
//var dataBase = require('dataBase');
//dataBase('create', null, null, null);
//var signos = ['aries','taurus','gemini','cancer','leo','virgo','libra','scorpio','sagitarius','capricorn','aquarius','pisces'];
var countt = 0;
exports.receberSignos = function(signo, contador) {
signos = signo;
countt = contador;
};
exports.getHoroscopo = function(callback) {
var url = 'http://theastrologer-api.herokuapp.com/api/horoscope/' + signos + '/today';
xhr.open('GET', url);
alert(countt);
xhr.onload = function() {
var data = JSON.parse(this.responseText);
callback(data);
};
Ti.API.info('ss ' + countt + ' signos ' + signos);
xhr.onerror = function(e) {
alert(e.error);
};
xhr.send();
};
The class that calls the backend and inserts:
signos.forEach(function(value, index) {
Ti.API.info('index ' + index + ' value ' + value);
if (index < 12) {
backend.receberSignos(value, index);
backend.getHoroscopo(function(sunsign) {
Ti.API.info('signo2 ' + sunsign.sunsign + ' cont ' + cont);
dataBase('insert', sunsign.sunsign, sunsign.date, sunsign.horoscope, a.toString());
a++;
if (cont == true) {
carregar();
}
});
}
});