How to return just User document and relationships in Mongoose with Express

I am going crazy i want to learn but i just can’t understand! I have an app in express and every time i try to do a query it return this:

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
  _executionCount: 0,
  mongooseCollection: NativeCollection {
    collection: Collection { s: [Object] },
    Promise: [Function: Promise],
    modelName: 'user',
    _closed: false,
    opts: {
      autoIndex: true,
      autoCreate: undefined,
      schemaUserProvidedOptions: [Object],
      capped: false,
      Promise: [Function: Promise],
      '$wasForceClosed': undefined
    },
    name: 'users',
    collectionName: 'users',
    conn: NativeConnection {
      base: [Mongoose],
      collections: [Object],
      models: [Object],
      config: [Object],
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 1,
      _closeCalled: undefined,
      _hasOpened: true,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false,
      _connectionString: 'mongodb://localhost/server',
      _connectionOptions: [Object],
      name: 'server',
      host: 'localhost',
      port: 27017,
      user: undefined,
      pass: undefined,
      client: [MongoClient],
      '$initialConnection': [Promise],
      db: [Db]
    },
    queue: [],
    buffer: false,
    emitter: EventEmitter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      [Symbol(kCapture)]: false
    }
  },
  model: Model { user },
  schema: Schema {
    obj: {
      auth: [Array],
      email: [Array],
      phone: [Array],
      details: [Array],
      address: [Array],
      doc: [Array],
      notifications: [Array]
    },
    paths: {
      auth: [SchemaArray],
      email: [SchemaArray],
      phone: [SchemaArray],
      details: [SchemaArray],
      address: [SchemaArray],
      doc: [SchemaArray],
      notifications: [SchemaArray],
      _id: [ObjectId],
      updatedAt: [SchemaDate],
      createdAt: [SchemaDate],
      __v: [SchemaNumber]
    },
    aliases: {},
    subpaths: {
      'auth.$': [ObjectId],
      'email.$': [ObjectId],
      'phone.$': [ObjectId],
      'details.$': [ObjectId],
      'address.$': [ObjectId],
      'doc.$': [ObjectId],
      'notifications.$': [ObjectId]
    },
    virtuals: { id: [VirtualType] },
    singleNestedPaths: {},
    nested: {},
    inherits: {},
    callQueue: [],
    _indexes: [],
    methods: { initializeTimestamps: [Function (anonymous)] },
    methodOptions: {},
    statics: {},
    tree: {
      auth: [Array],
      email: [Array],
      phone: [Array],
      details: [Array],
      address: [Array],
      doc: [Array],
      notifications: [Array],
      _id: [Object],
      updatedAt: [Function: Date],
      createdAt: [Function: Date],
      __v: [Function: Number],
      id: [VirtualType]
    },
    query: {},
    childSchemas: [],
    plugins: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
    '$id': 1,
    mapPaths: [],
    s: { hooks: [Kareem] },
    _userProvidedOptions: { timestamps: true },
    options: {
      typePojoToMixed: true,
      typeKey: 'type',
      id: true,
      noVirtualId: false,
      _id: true,
      noId: false,
      validateBeforeSave: true,
      read: null,
      shardKey: null,
      autoIndex: null,
      minimize: true,
      discriminatorKey: '__t',
      optimisticConcurrency: false,
      versionKey: '__v',
      capped: false,
      bufferCommands: true,
      strictQuery: false,
      strict: true,
      timestamps: true,
      pluralization: true
    },
    '$timestamps': { createdAt: 'createdAt', updatedAt: 'updatedAt' },
    '$globalPluginsApplied': true,
    _requiredpaths: [],
    _indexedpaths: []
  },
  op: 'find',
  options: {},
  _conditions: { _id: 618a7023bb27f9270856a4a0 },
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: NodeCollection {
    collection: NativeCollection {
      collection: [Collection],
      Promise: [Function: Promise],
      modelName: 'user',
      _closed: false,
      opts: [Object],
      name: 'users',
      collectionName: 'users',
      conn: [NativeConnection],
      queue: [],
      buffer: false,
      emitter: [EventEmitter]
    },
    collectionName: 'users'
  },
  _traceFunction: undefined,
  '$useProjection': true
}

i just want to return the user and the relationships with the user nothing more. Help i find kind lost =(

the query i try to do is like User.findOne({_id: user._id})
if i try to
const user = User.findOne({_id: user._id})
const user.toJson() it gives error

     TypeError: user.toJson is not a function

const user = User.find( {_id: user._id} )

Where are you getting the user._id variable? Is it set?

Don’t you need to do await?

This

const user = await User.findOne({_id: user._id})

rather than

const user = User.findOne({_id: user._id})

1 Like