I want to request data with multiple IDs. Then I want to put the data into a dictionary in a way that the value for a key is an object that contains data requested with one of the IDs. As a result I would have a dictionary filled with key : value -pairs where the value is the object containing the data requested with the ID explained before. Example:
{
"id1": {
"row1": [{
"description": "foofoo",
"value": "barbar"
}],
"row2": [{
"description": "foofoo",
"value": "barbar"
}]
},
"id2": {
"row1": [{
"description": "foofoo",
"value": "barbar"
}],
"row2": [{
"description": "foofoo",
"value": "barbar"
}]
}
}
With the following function I save multiple IDs to an array. Those IDs are requested from database with requestIds() function.
saveIdsToArray(): Promise<string[]> {
let self = this;
return new Promise( function (resolve, reject) {
if (self.ids.length === 0) {
self.requestIds()
.subscribe(
(response: any) => {
response.map((dataObject: {
id: string
}) => {
self.ids.push(dataObject.id);
});
resolve(self.ids);
},
(error: any) => console.log("error : " + error)
);
}
});
}
And here I'm looping the requestIdData() function with the length of the ID array and want to save the result data into dataDict.
getIdData(): Promise<any> {
let self = this;
let promise: Promise<any>;
if (this.dataDict !== {}) {
this.dataService.getId().then(data => {
for (let i = 0; i < data.length; i++) {
promise = new Promise( function (resolve, reject) {
self.dataService.requestIdData(data[i])
.subscribe(
(response: any) => {
response.map((dataObject: { description: string,
value: string }) => {
self.idData.push({
description: dataObject.description,
value: dataObject.value
});
});
},
(error: any) => console.log("error : " + error)
);
self.dataDict[i] = self.idData;
console.log(self.dataDict);
});
}
});
}
return promise;
}
But for some reason I get the data from all the requests with different IDs into same array and then when it is supposed to create another array into dictionary it also has the same data again. Example:
{
"id1": {
"row 1": [{
"description": "foofoo",
"value": "barbar"
}],
"row 2": [{
"description": "foofoo",
"value": "barbar"
}],
"row 1": [{
//and here is the data requested with another id
}], //etc..
},
"id2": {
//same data than above
}
}
I need to get the the data from different IDs separated. What to do?
Aucun commentaire:
Enregistrer un commentaire