Mongoose populate() returns empty array Mongoose populate() returns empty array mongoose mongoose

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

  1. Get all the clientApps

  2. Populate the appClients

  3. Find the clientApp whose token matches the authHeader