When I receive an object from an API, I am creating new objects inside it using other API responses. For example:
API Response 1: Obj: {a: 1, b: 2}
API Response 2: 3
Creating an object: Obj.c = 3
Final result: Obj: {a: 1, b: 2, c: 3}
Issue:
console.log(Obj) returns Obj: {a: 1, b: 2, c: 3}
console.log(Obj.c) returns undefined
After using .map
on the properties and trying to access the created properties with a console.log
, they return undefined
. However, when logging any object, the created properties are present.
My Code:
async getGeneralInfo(token, seed) {
try {
API_HEADER.headers.Authorization = token;
let responseAvaliableCoins = await axios.get(
BASE_URL + "/coin",
API_HEADER
);
let avaliableCoins = responseAvaliableCoins.data.data.coins;
avaliableCoins.map(async (coin, index) => {
if (coin.status === "active") {
let responseCreateAddress = await axios.post(
BASE_URL + "/coin/" + coin.abbreviation + "/address",
{ seed },
API_HEADER
);
avaliableCoins[index].address =
responseCreateAddress.data.data.address;
let responseBalance = await axios.get(
BASE_URL +
"/coin/" +
coin.abbreviation +
"/balance/" +
coin.address,
API_HEADER
);
avaliableCoins.token = responseBalance.headers[HEADER_RESPONSE];
avaliableCoins[index].balance = responseBalance.data.data;
} else {
avaliableCoins[index].address = undefined;
avaliableCoins[index].balance = undefined;
}
});
console.warn(avaliableCoins[0].balance); //undefined
console.warn(avaliableCoins[0]["balance"]); //undefined
console.warn(avaliableCoins[0].address); //undefined
console.warn(avaliableCoins[0]["address"]); //undefined
console.warn("avaliableCoins", avaliableCoins); //All objects are here
console.warn("avaliableCoins[0]", avaliableCoins[0]); //All objects are here
return avaliableCoins;
} catch (error) {
internalServerError();
return;
}
}
update ----- https://i.sstatic.net/Glx4Z.png