I'm stuck in async
hell:
function convertToDomainUsers(dbUsers: Array<UserDB>): Array<UserDomain> {
// iterate each DB user and convert them to domain user types
const domainUsers: Array<UserDomain> = dbUsers.map( async (dbUser: UserDB) => {
// first convert the DB user to a Domain User
const domainUser: UserDomain = newUserDomainModel(dbUser);
// Now we need to get their user links from the DB
const dbUserLinks: Array<UserLinkDB> = await findDBUserLinks(dbUser.user_id);
// convert their user links to Domain user links
const domainUserLinks: Array<UserLinkDomain> = convertToUserLinks(dbUserLinks);
// now merry up the domain user links to the domain user
domainUser.links = domainUserLinks;
return domainUser;
});
return domainUsers;
}
function newUserDomainModel(user: UserDB): UserDomain {
const domainUser: UserDomain = {
username: user.user_username,
firstName: user.user_name_first,
lastName: user.user_name_last
};
return domainUser;
}
async function findDBUserLinks(userId: bigint): Promise<Array<UserLinkDB>> {
const dbUserLinks: Array<UserLinkDB> = await getUserLinks(userId);
return dbUserLinks;
}
async function getUserLinks(id: bigint): Promise<Array<UserLinkDB>> {
setDB();
await client.connect();
const query = `
select
link_url,
social_type_id
from user_links
WHERE user_id = ${id}`;
const res = await client.query(query);
const links: Array<UserLinkDB> = res.rows;
return Promise.resolve(links);
}
Error (happening on const domainUsers:
in the convertToDomainUsers
function):
TS2322: Type 'Promise<UserDomain>[]' is not assignable to type 'UserDomain[]'. Type 'Promise<UserDomain>' is missing the following properties from type 'UserDomain': username, firstName, lastName, fullName, and 6 more
comments were added for the sake of making this stack post easier to follow. I don't normally write comments, they're cruft.