Mongoose populate() returns empty array
The findOne()
function return only one document. After that the populate method populates the sub document value. But the next findOne()
doesn't work, so you get a null
response.
You could try this instead:
async function performAuth(){ let authHeader = authHeader.replace("Basic ", ""); authHeader = Buffer.from(authHeader, 'base64').toString('ascii'); const clientApp = await clientApp.find({}) .populate('appClients') .map(data => data.find(item=>item.appClients.token === authHeader).exec();}
Operations Performed
Get all the clientApps
Populate the appClients
Find the clientApp whose token matches the authHeader