0

I am tryin to process the data found from a query. however, but I am not able to get back the results from it for some reason.

I am able to see it found 2 records from the query, but I cant get the actual records. not sure where I am messing up here

  const channelQuery =  Channel.find({ fields: { $all: channelFieldsFieldNames } })


  let fetchedChannels;
  channelQuery
    .then(documents => {
      fetchedChannels = documents;
      return Channel.count();
    })
    .then(count => {
      console.log({
        message: "Channels fetched successfully!",
        channels: fetchedChannels,
        maxChannels: count
      });
    })
    .catch(error => {
      console.log(error)
      res.status(500).json({
        message: "Fetching channels failed!"
      });
    });

this is what i see in the terminal:

{
  message: 'Channels fetched successfully!',
  channels: [],
  maxChannels: 2
}

its weird because I use the same snip of code in other places in my app and it returns the count and the records perfectly, but not this time

so i just want to know how to process the records from the response

i found a few similar stackoverflow posts but they didnt did not discuss how they process the data, they only talked about the query itself How to query MongoDB with "like"

Juan Casas
  • 268
  • 2
  • 13

1 Answers1

1

You are using two different querys:
return Channel.count(); => here you count all the Channels in mongo
while your search const channelQuery = Channel.find({ fields: { $all: channelFieldsFieldNames } }) finds nothing

Taymer
  • 304
  • 2
  • 7