I have an object outside of the function that I want to populate with every iteration and call that object after the iteration is done but I am only able to see the last element in my object. How do I add to object instead of replacing?
My code:
let myobj = {};
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
myobj["name"] = response.data.name;
myobj["id"] = table_id;
await func2({ query: graphQL_query, table_id });
}
const func2 = async (result, table_id) => {
let dev = await getAllTables({ query: tables(123) });
let prod = await getAllTables({ query: tables(321) });
let prod_id = new Set();
result.data.data.table.table_fields.forEach((d) => {
if (d. connector != null && !(d.name in dev))
prod_id.add(prod[d.name]["id"]);
});
prod_id = [...prod_id];
prod_id.map((x) => createOne({ query: graphQL_query(x) });
return Promise.all(prod_id.map((x) => func1({ query: graphQL_query(x) }));
});
const callback = () => console.log(myobj);
const execute = async () => {
await func1()
callback()
}
myobj
is returning the last element in the object rather than the 5 elements it is supposed to have been populated with.
How do I get my object to have all the elements?
You need an array to hold multiple objects.
let myarray = [];
const func1 = async (arg) => {
let table_id = result.data.internal_id;
const response = await axios_Func({ graphQL query });
console.log(`${response.data.name} - ${table_id}`);
const myobj = {};
myobj["name"] = response.data.name;
myobj["id"] = table_id;
myarray.push(myobj);
await func2({ query: graphQL_query, table_id });
}