MongoDB .find() doesn't return the right data

Issue

I’m trying get the data from mongodb but whenever i try to use find() I get following response. I’m very new to Mongo and just don’t know why this is happening. Also this code is written in TypeScript and used to code a Discord bot.

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
  _executionStack: null,
  mongooseCollection: Collection {
    collection: null,
    Promise: [Function: Promise],
    modelName: 'verifiedUsers',
    _closed: false,
    opts: {
      autoIndex: true,
      autoCreate: true,
      schemaUserProvidedOptions: {},
      capped: false,
      Promise: [Function: Promise],
      '$wasForceClosed': undefined
    },
    name: 'verifiedUsers',
    collectionName: 'verifiedUsers',
    conn: NativeConnection {
      base: [Mongoose],
      collections: [Object],
      models: [Object],
      config: {},
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 0,
      _closeCalled: false,
      _hasOpened: false,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false
    },
    queue: [],
    buffer: true,
    emitter: EventEmitter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      [Symbol(kCapture)]: false
    }
  },
  model: Model { verifiedUsers },
  schema: Schema {
    obj: { ign: [Object], memberid: [Object] },
    paths: {
      ign: [SchemaString],
      memberid: [SchemaString],
      _id: [ObjectId],
      __v: [SchemaNumber]
    },
    aliases: {},
    subpaths: {},
    virtuals: { id: [VirtualType] },
    singleNestedPaths: {},
    nested: {},
    inherits: {},
    callQueue: [],
    _indexes: [],
    methods: {},
    methodOptions: {},
    statics: {},
    tree: {
      ign: [Object],
      memberid: [Object],
      _id: [Object],
      __v: [Function: Number],
      id: [VirtualType]
    },
    query: {},
    childSchemas: [],
    plugins: [ [Object], [Object], [Object], [Object], [Object] ],
    '$id': 1,
    mapPaths: [],
    s: { hooks: [Kareem] },
    _userProvidedOptions: {},
    options: {
      typeKey: 'type',
      id: true,
      _id: true,
      validateBeforeSave: true,
      read: null,
      shardKey: null,
      discriminatorKey: '__t',
      autoIndex: null,
      minimize: true,
      optimisticConcurrency: false,
      versionKey: '__v',
      capped: false,
      bufferCommands: true,
      strictQuery: true,
      strict: true,
      pluralization: true
    },
    '$globalPluginsApplied': true
  },
  op: 'find',
  options: {},
  _conditions: {},
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: NodeCollection {
    collection: Collection {
      collection: null,

This is the code I’m using:
Schema:

import mongoose from 'mongoose'

const verifiedUsers = new mongoose.Schema({
    ign: {
        type: String,
        required: true
    },
    memberid: {
        type: String,
        required: true
    },
})

export default mongoose.model('verifiedUsers', verifiedUsers, 'verifiedUsers')

get Verfifed Users:

import mongoose from 'mongoose'
import verifiedUsers from './schemas/verified-users'

client.on('ready', async () => {
    await mongoose.connect(
        process.env.mongo_uri || '',
        {
            keepAlive: true
    })
})

async function getVerifiedUsers() {
    console.log(verifiedUsers.find())
}
getVerifiedUsers()

Why is this happening and how can I prevent this?

Solution

It seems like you need to call .exec().

Check Model.find() and this answer.

Maybe someone else can explain why it was decided to do it like this instead of awaiting .find() to get the data.

Answered By – Daviid

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.