I'm building a blog and for each blog post I'm trying to include the authors name, rather than the authors ID.
I have two functions: getUserName
and mapData
.
mapData remaps the blog object:
function mapData(posts) {
let result = [];
result = posts.map(post => ({
id: post._id,
title: post.title,
imageURL: post.imageURL,
body: post.body,
author: getUserName(post.createdBy),
date: date(post.createdAt)
}));
console.log(result)
return result;
}
getUserName gets the users name:
const getUserName = async id => {
try {
const getUser = await User.findById(id)
console.log(getUser)
return getUser.firstname;
} catch (err) {
throw err;
}
}
But after console logging the result in mapData
the value for author
is Promise { <pending> }
. Can anyone explain why this is happening? I thought by using an async
function would avoid this?