how to call test() function could you put code here and also how to print result
@AaronSterling please I do not master the Promise can make an example for my case
Are currencies and val both type number? (I.e. currencies array stores numbers)
My provider class
import {Injectable} from '@angular/core';
import {Storage} from '@ionic/storage';
import {AlertController, Events} from 'ionic-angular';
import {Const} from "../../app/app.global";
import {HttpClient, HttpParams} from "@angular/common/http";
import {ToastProvider} from "../boutique/toast/toast";
import {FileTransfer, FileTransferObject, FileUploadOptions} from "@ionic-native/file-transfer";
import {OutilProvider} from "../outil/outil";
@Injectable()
export class UserProvider {
private USER_KEY: string = 'user';
private CURENT_CURRENCY_KEY: string = 'currency';
private CURRENCES_KEY: string = 'currenceis';
user: any = {};
private static result: any = 0;
constructor(private transfer: FileTransfer, public storage: Storage,
public events: Events, private outil: OutilProvider, private alertCtrl: AlertController,
private http: HttpClient, private toas: ToastProvider) {
this.load();
}
load() {
return this.storage.get(this.USER_KEY).then((val) => {
if (val)
this._loggedIn(val, 0);
});
}
setCurrentcurrency(data) {
this.storage.set(this.CURENT_CURRENCY_KEY, data);
this.http.get(Const.url + '/?currency=' + data).subscribe(res => {
});
this.storage.set(this.CURENT_CURRENCY_KEY, data);
}
getCurrentCurrency() {
return this.storage.get(this.CURENT_CURRENCY_KEY);
}
updateCurrency() {
let cookie = this.user.cookie;
this.http.get(Const.url + '/api/user/getcurrency/?insecure=cool&cookie=' + cookie).subscribe(val => {
this.storage.set(this.CURRENCES_KEY, val);
});
}
changeCurrentCurrency(value: number):Promise<any>{
return new Promise(resolve => {
this.getCurrency().then((currencies) => {
this.getCurrentCurrency().then((val) => {
let res = value * currencies[val]['rate'];
return res;
});
});
});
}
}
My pipe class
import {Pipe, PipeTransform} from '@angular/core';
import {CurrencyPipe} from '@angular/common';
import {SettingsProvider} from '../../providers/providers';
import {Storage} from "@ionic/storage";
import {UserProvider} from "../../providers/user/user";
@Pipe({
name: 'money',
})
export class MoneyPipe implements PipeTransform {
app: any = {};
current;
constructor(public setting: SettingsProvider, private user: UserProvider) {
this.setting.load().then(x => {
if (x) this.app = x.settings;
})
}
transform(value, app) {
let x = app || this.setting.all.settings;
this.user.changeCurrentCurrency(value).then((res) => {
console.log(res);
});
return new CurrencyPipe(x.currency).transform(value, x.currency, 'symbol-narrow', '1.' + x.number_of_decimals + '-' + x.number_of_decimals, 'fr');
}
}
val is string example EUR
currencies is a json EUR is a key currencies example
EUR :{name: "EUR", rate: 1, symbol: "€", position: "right_space", is_etalon: 1, hide_cents: 0,…}
Create a formal type for it. There are a lot of issues in your code, but you can fix a lot of them if you step back and define a type for every variable. Also, don’t use var. Use let. Only use var if you understand the difference and are in a special situation where var is better. Then
test(value: TypeOfValue): Promise<number> {
let currencies = this.storage.get('age');
let betterVariableNameThanVal = this.getCurrentCurrency();
return Promise.all(currencies, betterVariableNameThanVal)
.then(resultArray => value * resultArray[0][resultArray[1]]['rate']);
}
@AaronSterling Promise.all asks me for an argument
return Promise.all (currencies, betterVariableNameThanVal
.then(resultArray => value * resultArray[0][resultArray[1]][‘rate’]);
Look for typos…
Good @AaronSterling your solution ist correct thanks. youare forget Promise.all()
Good evening my friends please I need your help on the promised I still have problems, I want to execute the function changeCurrentCurrency in my pipe class and back the result but since nothing at all:
- My provider class
import {Injectable} from '@angular/core';
import {Storage} from '@ionic/storage';
import {AlertController, Events} from 'ionic-angular';
import {Const} from "../../app/app.global";
import {HttpClient, HttpParams} from "@angular/common/http";
import {ToastProvider} from "../boutique/toast/toast";
import {FileTransfer, FileTransferObject, FileUploadOptions} from "@ionic-native/file-transfer";
import {OutilProvider} from "../outil/outil";
@Injectable()
export class UserProvider {
private USER_KEY: string = 'user';
private CURENT_CURRENCY_KEY: string = 'currency';
private CURRENCES_KEY: string = 'currenceis';
user: any = {};
constructor(private transfer: FileTransfer, public storage: Storage,
public events: Events, private outil: OutilProvider, private alertCtrl: AlertController,
private http: HttpClient, private toas: ToastProvider) {
this.load();
}
load() {
return this.storage.get(this.USER_KEY).then((val) => {
if (val)
this._loggedIn(val, 0);
});
}
_loggedIn(user, index) {
this.user = user;
this.events.publish('user:login', {tabIndex: index});
return this.user;
}
setCurrentcurrency(data) {
this.storage.set(this.CURENT_CURRENCY_KEY, data);
this.http.get(Const.url + '/?currency=' + data).subscribe(res => {
});
this.storage.set(this.CURENT_CURRENCY_KEY, data);
}
getCurrentCurrency() {
return this.storage.get(this.CURENT_CURRENCY_KEY);
}
updateCurrency() {
let cookie = this.user.cookie;
this.http.get(Const.url + '/api/user/getcurrency/?insecure=cool&cookie=' + cookie).subscribe(val => {
this.storage.set(this.CURRENCES_KEY, val);
});
}
changeCurrentCurrency(value: number): Promise<number> {
let currencies = this.getCurrency();
let betterVariableNameThanVal = this.getCurrentCurrency();
return Promise.all([currencies, betterVariableNameThanVal])
.then(resultArray => value * resultArray[0][resultArray[1]]['rate']);
}
}
-My pipe classe
import {Pipe, PipeTransform} from '@angular/core';
import {CurrencyPipe} from '@angular/common';
import {SettingsProvider} from '../../providers/providers';
import {Storage} from "@ionic/storage";
import {UserProvider} from "../../providers/user/user";
@Pipe({
name: 'money',
})
export class MoneyPipe implements PipeTransform {
app: any = {};
current;
constructor(public setting: SettingsProvider, private user: UserProvider) {
this.setting.load().then(x => {
if (x) this.app = x.settings;
})
}
transform(value:number, app) {
let x = app || this.setting.all.settings;
this.user.changeCurrentCurrency(value).then((val => {
return new CurrencyPipe(x.currency).transform(val, x.currency, 'symbol-narrow', '1.' + x.number_of_decimals + '-' + x.number_of_decimals, 'fr');
}));
}
}