简体   繁体   中英

How to process the data found from a mongodb query using node.js and mongoose

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM