This post was flagged by the community and is temporarily hidden.
Holy nesting, Batman!
So below is what I propose. First, there’s less nesting and no more nested promises . Second, I moved the POST request outside of your for loop, as it seems you want to sync all contacts at once rather than one at a time while building the array
. Third, I don’t think you need to manually convert parameters to JSON with
http.post
.
postRequest() {
return this.sqlite.create({
name: 'ionicdb.db',
location: 'default'
})
.then(db => db.executeSql('SELECT * FROM Contactos ORDER BY rowid DESC', {}))
.then(result => {
this.contactos = [];
for(var i = 0; i< result.rows.length; i++) {
this.contactos.push({
rowid:result.rows.item(i).rowid,
nombre:result.rows.item(i).nombre,
telefono:result.rows.item(i).telefono,
ext:result.rows.item(i).ext,
correo:result.rows.item(i).correo,
empresa:result.rows.item(i).empresa
});
}
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let params = {
nombre: 'Sergio',
telefono: '1234567',
ext: '000',
correo: 'zergiodkamargo.sc@gmail.com',
empresa: 'Globaltek'
};
return this.http.post("http://181.48.244.108/FeriaDutotec/api/Contactos1", params, options).toPromise();
})
.then(contactos => {
this.contactos.push(contactos.json())
console.log(contactos ['_body']);
this.toast.show('Datos Sincronizados!' , '4000', 'center').subscribe(toast => console.log(contactos))
}, error => {
console.log(error.message);// Error obteniendo los datos!
this.toast.show('Error en la sincronizacion!'+ error.message, '4000' , 'center').subscribe(toast => console.log(toast))
})
.catch(err => console.error(err));
}
The array:
public contactos: any [] = [];
Not Works Show this message: Error en la sinicronizacion! undefined.
Bet this is s CORS Problem. Add allow origin at the server side