code in promise .then firing way before promise finishes - arrays

I have this code...sorry for the messiness I've been at this a while:
loadAvailabilities() {
let promises = [];
let promises2 = [];
let indexi = 0;
//return new Promise((resolve, reject) => {
this.appointments = this.af.list('/appointments', { query: {
orderByChild: 'selected',
limitToFirst: 10
}});
let mapped;
this.subscription2 = this.appointments.subscribe(items => items.forEach(item => {
//promises.push(new Promise((resolve, reject) => {
console.log(item);
let userName = item.$key;
//this.availabilities = [];
for(let x in item) {
let month = x;
console.log(x + " month");
this.appointmentsMonth = this.af.list('/appointments/' + userName + '/' + month);
this.subscription3 = this.appointmentsMonth.subscribe(items => items.forEach(item => {
this.startAtKeyAvail = item.$key;
//console.log(JSON.stringify(item) + " item");
let date = new Date(item.date.day * 1000);
let today = new Date();
console.log(date.getMonth() + "==" + today.getMonth() + "&&" + date.getDate() + "==" + today.getDate());
console.log("IN LOAD AVAILABILITIES *(*((**(*(*(*(*(*(*&^^^^%^%556565656565");
if(date.getMonth() == today.getMonth() && date.getDate() == today.getDate()) {
console.log(" inside the if that checks if its today");
console.log(item.reserved.appointment + " *************appointment");
//let counter = 0;
//mapped = item.reserved.appointment.map((r) => {
//item.reserved.appointment.forEach((r, index) => {
for(let r of item.reserved.appointment) {
promises.push(new Promise((resolve, reject) => {
if(r.selected == true) {
//this.renderer.setElementStyle(this.noavail.nativeElement, 'display', 'none');
let storageRef = firebase.storage().ref().child('/settings/' + userName + '/profilepicture.png');
let obj = {'pic':"", 'salon': userName, 'time': r.time};
storageRef.getDownloadURL().then(url => {
console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!");
obj.pic = url;
this.availabilities.push(obj);
console.log(JSON.stringify(this.availabilities));
resolve();
}).catch((e) => {
console.log("in caught url !!!!!!!$$$$$$$!!");
obj.pic = 'assets/blankprof.png';
this.availabilities.push(obj);
console.log(JSON.stringify(this.availabilities));
resolve();
});
}
}))
}
}
}))
}
}))
//}));
Promise.all(promises).then(() => {
console.log("in load availabilities ......... ")
console.log(JSON.stringify(this.availabilities));
this.availabilities.sort(function(a,b) {
return Date.parse('01/01/2013 '+a.time) - Date.parse('01/01/2013 '+b.time);
});
console.log('*****previous******');
console.log(JSON.stringify(this.availabilities));
console.log('*****sorted********');
for(let i of this.availabilities) {
console.log(i.time + " this is itime");
let date = new Date('01/01/2013 ' + i.time);
console.log(date + " this is date in idate");
let str = date.toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric' });
console.log(str);
i.time = str;
}
});
//}))
//})
}
I can tell from the log messages that the storageRef.getDownloadURL() function happens close to the end of when my page loads...this is where the objects actually get pushed to this.availabilities (eventually used to populate a list). The code in the Promise.all .then() actually fires before anything gets pushed to this.availabilities so when the sorting happens it is an empty array and nothing gets sorted.

I used a promise inside each forEach loop. I pushed all the promises to the same array and used Promise.all(array).then(...) how it is being used above - and the promises did their job - the array is sorted because everything is happening asynchronously.

Related

Cannot set setstate in reactJS

Can anybody explain what im doing wrong here, my setState console is returning undefined where the first console is returning right values
return firebase.database().ref('Users/Trainers/').on('value', (snapshot) => {
snapshot.forEach(function(childSnapshot){
// var childKey= childSnapshot.key;
var childData= childSnapshot.val();
var childEmail = childData.email;
var childfirstName = childData.firstName;
var childlastName = childData.lastName;
var childTrainers = childfirstName + ' ' + childlastName + ' ' + childEmail;
console.log(childTrainers);
})
this.setState({
Trainers: snapshot.val().childTrainers
})
console.log(this.state.Trainers)
})
Okay ive done, and the correct answer is
return firebase.database().ref('Users/Trainers/').on('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
// this.setState({
// childEmail = childSnapshot.val().email
// })
// var childKey= childSnapshot.key;
var childData= childSnapshot.val();
var childEmail = childData.email;
var childfirstName = childData.firstName;
var childlastName = childData.lastName;
var childTrainers = childfirstName + ' ' + childlastName + ' ' + childEmail;
//console.log(childTrainers);
this.setState({
Trainers: [childTrainers]
}, console.log(this.state.Trainers))
})
})
All values are retrieving

Avoid message repeating?

I need some help here I have a code that shows a message being updated per 2 seconds but I want a single message that replaces itself.
let channel = guild.channels.find(channel => channel.name === guilds[guild.id].digitchan);
let channel2 = guild.channels.find(channel => channel.name === guilds[guild.id].countdownchan);
if (channel && channel2) {
channel.send(embed);
scrims[guild.id] = {
timer: setTimeout(function() {
channel2.join().then(connection => {
const dispatcher = connection.playFile('./Audio/test.mp3');
console.log('dispatcher');
console.log(dispatcher == null);
dispatcher.on('end', () => {
channel2.leave();
const embed2 = new RichEmbed()
.setColor(0xc1d9ff)
.addField("message x", false);
channel.send(embed2);
scrims[guild.id].codes = true;
scrims[guild.id].codedata = {};
scrims[guild.id].playerinterval = setInterval(function() {
const embed4 = new RichEmbed()
.setColor(0xc1d9ff)
.setTitle("codes:");
Object.keys(scrims[guild.id].codedata).forEach(function(key) {
let codeobj = scrims[guild.id].codedata[key];
let user_str = "";
Object.keys(scrims[guild.id].codedata[key]).every(function(key2, count) {
let user = scrims[guild.id].codedata[key][key2];
user_str = user_str + user + "\n"
if (count >= 15) {
if (count > 15) user_str = user_str + "and more";
return false;
};
})
embed4.addField(key + " (" + Object.keys(codeobj).length + " codes)", user_str, true);
})
channel.send(embed4);
}, 2000);
scrims[guild.id].timer2 = setTimeout(function() {
scrims[guild.id].codes = false;
clearInterval(scrims[guild.id].playerinterval);
const embed3 = new RichEmbed()
.setColor(0xc1d9ff)
.setTitle("codes:");
Object.keys(scrims[guild.id].codedata).forEach(function(key) {
let codeobj = scrims[guild.id].codedata[key];
let user_str = "";
Object.keys(scrims[guild.id].codedata[key]).every(function(key2, count) {
let user = scrims[guild.id].codedata[key][key2];
user_str = user_str + user + "\n"
if (count >= 15) {
if (count > 15) user_str = user_str + "y mas..";
return false;
};
})
embed3.addField(key + " (" + Object.keys(codeobj).length + " codes)", user_str, true);
})
channel.send(embed3);
}, 1 * 60000);
});
});
}, time * 60000);
};
};
This is the discord action:
Codes:
Codes:
Codes:
Codes:
Codes:
Codes:
Codes:
Codes: 3c5
Codes: 3c5
So could be some kind of message deleting before sending the same message updated again, please let me know how to do it or some kind of code changing.
Just use message.edit() to edit the message.
message.edit() on discord.js docs

Three promises with HTTP

I had to do three HTTP Post requests in my code. The first two work, I debug the code and they return the correct value, but the last one returns undefined.
I made these three requisitions due to one depending on the response of the other.
login button:
goToMenu() {
this.dados_login = [];
this.dados_login.push({
"CPF": this.cpfLogin,
"Senha": this.senhaLogin
})
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
let options = new RequestOptions({ headers: headers });
return new Promise((resolve, reject) => {
this.http.post(this.url, JSON.stringify(this.dados_login["0"]), options)
.toPromise()
.then((response) => {
var json_token = (response as any)._body;
var parsed = JSON.parse(json_token);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.token = arr[0];
this.carregaEmpresas();
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.error_login = arr[0];
this.presentAlert(this.error_login)
});
});
function that carries companies, the error occurs here because it is not returned nothing to it
carregaEmpresas(newpage: boolean = false) {
console.log(this.cpfLogin);
this.abreCarregando();
return new Promise((resolve, reject) => {
this.EmpresaProvider.getEmpresas(this.token, this.cpfLogin)
.then((response) => {
var json_emp = (response as any)._body;
var parsed = JSON.parse(json_emp);
var arr_emp = [];
for (var x in parsed) {
arr_emp.push(parsed[x]);
}
this.lista_empresas = arr_emp;
this.objEmp = [];
for (let i = 0; i < this.lista_empresas.length; i++) {
this.obj = {
label:
this.lista_empresas[i].Valor,
type: 'radio',
value: this.lista_empresas[i].Chave
}
this.objEmp.push(this.obj);
}
this.fechaCarregando();
this.selectEmpresa();
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.error_login = arr[0];
this.presentAlert(this.error_login)
});
});
provider role:
return new Promise((resolve, reject) => {
this.http.post(this.baseApiPath, JSON.stringify(this.cpf_usuario["0"]), options)
.toPromise()
.then((response) => {
var empresa = (response as any)._body;
var parsed = JSON.parse(empresa);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.empresa_cod = arr[0].Chave.split("/", 1);
var urlFilial = this.apiFilial + this.empresa_cod["0"];
return this.http.get(urlFilial, options);
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
return arr[0];
});
});
GetEmpresas Code:
getEmpresas(token: string, Cpf: string) {
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
headers.append('Authorization', 'bearer ' + token);
let options = new RequestOptions({ headers: headers });
this.cpf_usuario.push({ "Cpf": Cpf });
return new Promise(resolve => {
window.setTimeout(() => {
this.http.post(this.baseApiPath, JSON.stringify(this.cpf_usuario["0"]), options)
.toPromise()
.then((response) => {
var empresa = (response as any)._body;
var parsed = JSON.parse(empresa);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.empresa_cod = arr[0].Chave.split("/", 1);
var urlFilial = this.apiFilial + this.empresa_cod["0"];
return this.http.get(urlFilial, options)
.toPromise()
.then((response) => {
var json_emp = (response as any)._body;
var parsed = JSON.parse(json_emp);
var arr_emp = [];
for (var x in parsed) {
arr_emp.push(parsed[x]);
}
this.emp = arr_emp;
return arr_emp;
})
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
return arr[0];
});
}, 2000);
});
}
This is a draft because I'm not sure I've found the problem yet, but I need to share more code than will fit in a comment.
The first thing I would try is removing the new Promise because you aren't resolving or rejecting those promises anyway. I'd also try removing the window.setTimeout. At that point, it looks like getEmpresas will return a promise for the arr_emp that is the result of parsing the final get response. If you do that, then the then handler in carregaEmpresas will receive the arr_emp that was generated in getEmpresas, so you should just name the parameter arr_emp and not try to parse it again. The code at this point is:
class MyClass {
// Dummy declarations to suppress TypeScript errors
dados_login;
cpfLogin;
senhaLogin;
http;
url;
token;
error_login;
presentAlert;
abreCarregando;
lista_empresas;
objEmp;
obj;
fechaCarregando;
selectEmpresa;
EmpresaProvider: EmpresaProvider;
goToMenu() {
this.dados_login = [];
this.dados_login.push({
"CPF": this.cpfLogin,
"Senha": this.senhaLogin
})
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
let options = new RequestOptions({ headers: headers });
this.http.post(this.url, JSON.stringify(this.dados_login["0"]), options)
.toPromise()
.then((response) => {
var json_token = (response as any)._body;
var parsed = JSON.parse(json_token);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.token = arr[0];
this.carregaEmpresas();
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.error_login = arr[0];
this.presentAlert(this.error_login)
});
}
carregaEmpresas(newpage: boolean = false) {
console.log(this.cpfLogin);
this.abreCarregando();
this.EmpresaProvider.getEmpresas(this.token, this.cpfLogin)
.then((/*response*/ arr_emp) => {
/*
var json_emp = (response as any)._body;
var parsed = JSON.parse(json_emp);
var arr_emp = [];
for (var x in parsed) {
arr_emp.push(parsed[x]);
}
*/
this.lista_empresas = arr_emp;
this.objEmp = [];
for (let i = 0; i < this.lista_empresas.length; i++) {
this.obj = {
label:
this.lista_empresas[i].Valor,
type: 'radio',
value: this.lista_empresas[i].Chave
}
this.objEmp.push(this.obj);
}
this.fechaCarregando();
this.selectEmpresa();
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.error_login = arr[0];
this.presentAlert(this.error_login)
});
}
}
class EmpresaProvider {
// Dummy declarations to suppress TypeScript errors
cpf_usuario;
http;
baseApiPath;
empresa_cod;
apiFilial;
emp;
getEmpresas(token: string, Cpf: string) {
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
headers.append('Authorization', 'bearer ' + token);
let options = new RequestOptions({ headers: headers });
this.cpf_usuario.push({ "Cpf": Cpf });
return this.http.post(this.baseApiPath, JSON.stringify(this.cpf_usuario["0"]), options)
.toPromise()
.then((response) => {
var empresa = (response as any)._body;
var parsed = JSON.parse(empresa);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
this.empresa_cod = arr[0].Chave.split("/", 1);
var urlFilial = this.apiFilial + this.empresa_cod["0"];
return this.http.get(urlFilial, options)
.toPromise()
.then((response) => {
var json_emp = (response as any)._body;
var parsed = JSON.parse(json_emp);
var arr_emp = [];
for (var x in parsed) {
arr_emp.push(parsed[x]);
}
this.emp = arr_emp;
return arr_emp;
})
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
return arr[0];
});
}
}
Please try this code (integrated into your original program of course) and let me know if it works or otherwise what is the exact error and what line it occurs on.

Does an angular.forEach create it's own scope?

My code has this HTML snippet:
<div ng-repeat="wf in wos.word.wordForms">
{{ wf }}
<textarea ng-change="wf.statusId = 2"
ng-model="wf.definition">
</textarea>
...
In a service I have this:
wordFormCheckAndUpdate = (): ng.IPromise<any> => {
var self = this;
var promises = [];
angular.forEach(self.word.wordForms, function (wf, key) {
if (wf.statusId == Status.Dirty) {
if (wf.createdDate) {
var updatePromise = self.wordFormUpdateSubmit(wf);
promises.push(updatePromise);
} else {
var addPromise = self.wordFormAddSubmit(wf);
promises.push(addPromise);
}
}
});
return self.$q.all(promises);
};
wordFormAddSubmit and wordFormUpdateSubmit modify the data in wf:
wf = angular.copy(response.data);
wf.statusId = 3;
wf.abc = "test"
When one of these functions is called it does not seem to change what is displayed above the textarea and the statusId still shows as 2 and "test" does not appear. Does anyone have any ideas what might be happening?
Update. Here are the two functions that are called:
wordFormAddSubmit = (wf: IWordForm): ng.IPromise<any> => {
var self = this;
return self.$http({
url: self.ac.dataServer + "/api/WordForm/Post",
method: "POST",
data: wf
})
.then(
(response: ng.IHttpPromiseCallbackArg<IWordForm>): any => {
wf = angular.copy(response.data);
self.$timeout(function () {
wf.statusId = 3;
wf.sourceId = 999;
}, 3);
},
(error: ng.IHttpPromiseCallbackArg<any>): any => {
self.ers.error(error);
return self.$q.reject(error);
});
}
wordFormUpdateSubmit = (wf: IWordForm): ng.IPromise<any> => {
var self = this;
return self.$http({
url: self.ac.dataServer + "/api/WordForm/Put",
method: "PUT",
data: wf
})
.then(
(response: ng.IHttpPromiseCallbackArg<IWordForm>): any => {
wf = angular.copy(response.data);
//$timeout(function () {
wf.statusId = 3;
//}, 1);
var a = wf;
var b = wf;
},
(error: ng.IHttpPromiseCallbackArg<any>): any => {
self.ers.error(error);
return self.$q.reject(error);
});
}

How to make this asynchronous call, synchronous

I have an associative array date as key, the value in this case is an array of objects. Now in this object
while (tempDate < endDate) {
$scope.dates.push(tempDate.format("DD-MM-YYYY"));
var dailyTrips = response.data[tempDate.format("DD-MM-YYYY")];
for (var i = 0; i < dailyTrips.length; i++) {
// console.log(moment(dailyTrips[i].trip.startTime).format("hh:mm a"));
// console.log(moment(dailyTrips[i].trip.endTime).format("hh:mm a"));
geocode(dailyTrips, i);
}
tempDate.add(1, 'days');
}
$scope.data = response.data;
var geocode = function(dailyTrips, i) {
(function() {
var latlng = {
lat: dailyTrips[i].trip.source.latitude,
lng: dailyTrips[i].trip.source.longitude
};
console.log(latlng);
var geocoder = new google.maps.Geocoder;
geocoder.geocode({
'location': latlng
}, function(result, status) {
if (result[1]) {
console.log(result[1].address_components[0].long_name+" "+result[1].address_components[2].long_name);
}
});
}(i));
};
Now This code is working but running at completely different times as it's a callback. I wanted to have this all at one time so that I can use it in ng-repeat and show everything nicely. How do I do that?
Expected output
Trip StartTime Trip from
Trip EndLine Trip To
EDIT 1: Still not working
while (tempDate < endDate) {
$scope.dates.push(tempDate.format("DD-MM-YYYY"));
var dailyTrips = response.data[tempDate.format("DD-MM-YYYY")];
var promises = [];
for (var i = 0; i < dailyTrips.length; i++) {
promises[i] = geocode(dailyTrips, i);
}
$q.all(promises).then(function(result) {
console.log(result); //line 39
});
tempDate.add(1, 'days');
}
$scope.data = response.data;
}, function(error) {
console.log(error.data);
});
var geocode = function(dailyTrips, i) {
var q = $q.defer();
var latlng = {
lat: dailyTrips[i].trip.source.latitude,
lng: dailyTrips[i].trip.source.longitude
};
console.log(latlng);
var geocoder = new google.maps.Geocoder;
geocoder.geocode({
'location': latlng
}, function(result, status) {
console.log(status);
if (status === google.maps.GeocoderStatus.OK) {
console.log(result[1].address_components[0].long_name + " " + result[1].address_components[2].long_name);
q.resolve(result[1].address_components[0].long_name + " " + result[1].address_components[2].long_name);
} else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
setTimeout(function() {
geocode(dailyTrips, i);
}, 500);
} else {
q.reject();
}
});
return q.promise;
};
You cannot make an async call synchronous, except by blocking your code, which you do no NOT want to do. Instead, I suggest you convert geocode into a promise
var geocode = function(dailyTrips, i) {
return this.$q((resolve, reject) => {
....
and then wait for all the promises to complete
var promises = [];
for (var i = 0; i < dailyTrips.length; i++) {
promises[i] = geocode(dailyTrips, i);
}
$q.all(promises).then( function(results) {...} );

Resources