Cannot get data out of Promis.allSettled()

I am trying to get data from db which requires a an array of individual queries like this:
const dbColumns = await ctx.db
.select()
.from(columns)
.where(eq(columns.projectId, projectId));

const promises = dbColumns.map(async (column) => {
const dbItems = await ctx.db
.select()
.from(items)
.where(eq(items.columnId, column.id));

return {
...column,
items: dbItems,
};
});

const columnsWithItems: ColumnWithItems[] = [];

Promise.allSettled(promises)
.then((results) => {
results.map((result) => {
if (result.status === "fulfilled") {
const columnWithItems = result.value;
columnsWithItems.push(columnWithItems);
console.log(columnsWithItems);
}
});
})
.catch((err) => console.error(err));
console.log(columnsWithItems); // empty array


const dbColumns = await ctx.db
.select()
.from(columns)
.where(eq(columns.projectId, projectId));

const promises = dbColumns.map(async (column) => {
const dbItems = await ctx.db
.select()
.from(items)
.where(eq(items.columnId, column.id));

return {
...column,
items: dbItems,
};
});

const columnsWithItems: ColumnWithItems[] = [];

Promise.allSettled(promises)
.then((results) => {
results.map((result) => {
if (result.status === "fulfilled") {
const columnWithItems = result.value;
columnsWithItems.push(columnWithItems);
console.log(columnsWithItems);
}
});
})
.catch((err) => console.error(err));
console.log(columnsWithItems); // empty array


why is this happening?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server