I'm getting an object coming from an API and I'm creating new objects inside it with other API responses, ex:
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}
Problem:
console.log(Obj) return Obj: {a: 1, b: 2, c: 3}
console.log(Obj.c) return undefined
I am not getting when trying to give a console.log
after the .map
in the properties, I can not access the created properties, returns undefined
. However, when I give a console.log
on any object, the created properties are there.
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;
}
}