@metal_millitia

Получение данных из объекта mongoose?

Всем привет! На сервере, для доступа к API, после POST запроса к авторизации через username/password необходимо послать токен, который генерируется при регистрации. Чтобы его получить, провожу поиск по таблице с данным username, после чего пытаюсь извлечь токен, но с этим возникает проблема. На данный момент всё выглядит так:
var query = Account.findOne({username: req.user}).select('token');
        console.log(query);

Но ответ на данный запрос получаю такой:
Query {
  _mongooseOptions: {},
  mongooseCollection:
   NativeCollection {
     collection: Collection { s: [Object] },
     opts:
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'accounts',
     collectionName: 'accounts',
     conn:
      NativeConnection {
        base: [Mongoose],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'passport_local_mongoose_express4',
        options: [Object],
        otherDbs: [],
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        db: [Db] },
     queue: [],
     buffer: false,
     emitter:
      EventEmitter {
        _events: [Object: null prototype] {},
        _eventsCount: 0,
        _maxListeners: undefined } },
  model:
   { [Function: model]
     hooks: Kareem { _pres: {}, _posts: {} },
     base:
      Mongoose {
        connections: [Array],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        plugins: [Array] },
     modelName: 'accounts',
     model: [Function: model],
     db:
      NativeConnection {
        base: [Mongoose],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'passport_local_mongoose_express4',
        options: [Object],
        otherDbs: [],
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        db: [Db] },
     discriminators: undefined,
     '$appliedMethods': true,
     authenticate: [Function],
     serializeUser: [Function],
     deserializeUser: [Function],
     register: [Function],
     findByUsername: [Function],
     createStrategy: [Function],
     '$appliedHooks': true,
     _events:
      [Object: null prototype] { init: [Function], save: [Function] },
     _eventsCount: 2,
     schema:
      Schema {
        obj: [Object],
        paths: [Object],
        aliases: {},
        subpaths: {},
        virtuals: [Object],
        singleNestedPaths: {},
        nested: {},
        inherits: {},
        callQueue: [Array],
        _indexes: [],
        methods: [Object],
        statics: [Object],
        tree: [Object],
        query: {},
        childSchemas: [],
        plugins: [Array],
        s: [Object],
        _userProvidedOptions: undefined,
        options: [Object],
        '$globalPluginsApplied': true,
        _requiredpaths: [] },
     collection:
      NativeCollection {
        collection: [Collection],
        opts: [Object],
        name: 'accounts',
        collectionName: 'accounts',
        conn: [NativeConnection],
        queue: [],
        buffer: false,
        emitter: [EventEmitter] },
     Query: { [Function] base: [Query] },
     '$__insertMany': [Function],
     insertMany: [Function],
     '$init':
      Promise { emitter: [EventEmitter], emitted: [Object], ended: false } },
  schema:
   Schema {
     obj:
      { username: [Function: String],
        password: [Function: String],
        fio: [Function: String],
        role: [Function: String],
        token: [Function: String] },
     paths:
      { username: [SchemaString],
        password: [SchemaString],
        fio: [SchemaString],
        role: [SchemaString],
        token: [SchemaString],
        _id: [ObjectId],
        hash: [SchemaString],
        salt: [SchemaString],
        __v: [SchemaNumber] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [VirtualType] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue:
      [ [Array], [Array], [Array], [Array], [Array], [Array], [Array] ],
     _indexes: [],
     methods: { setPassword: [Function], authenticate: [Function] },
     statics:
      { authenticate: [Function],
        serializeUser: [Function],
        deserializeUser: [Function],
        register: [Function],
        findByUsername: [Function],
        createStrategy: [Function] },
     tree:
      { username: [Function: String],
        password: [Function: String],
        fio: [Function: String],
        role: [Function: String],
        token: [Function: String],
        _id: [Object],
        hash: [Object],
        salt: [Object],
        __v: [Function: Number],
        id: [VirtualType] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object], [Object], [Object] ],
     s: { hooks: [Kareem], kareemHooks: [Object] },
     _userProvidedOptions: undefined,
     options:
      { retainKeyOrder: false,
        typeKey: 'type',
        id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     '$globalPluginsApplied': true,
     _requiredpaths: [] },
  op: 'findOne',
  options: { retainKeyOrder: false },
  _conditions:
   { username:
      { _id: 5c9fbd55487df10cf03e24ad,
        salt:
         '9b261f8e3c9ab4a42fa2661cee2f3f1de52ee2ed1e2a0cdd8a6715607807cf84',
        hash:
         '45aff1cede13331174a266f7bc68c196d43747ef415eede5a2f5a8597463a60853fd386627060ba360f94cc856d83aa6cba72b3bb22e9f159b8df58b4dedd6d2edb2be4f1b7c7d4fc0ac756c225539473a9ac0ec480f234e562f05991ea2e15ef3f01c5f45d48bc362d48b8676ce2b5c15b458eba1193099ef40b9dd68e04dec1f003fc650a63676f2f3a37711314307c8c165c751415470eb8554f9ed088e15f65b05a71016a66fbe494adfc37e9c164647b667f787d63a92bb679bcbe542920540fbfe34d57a6b85f06051e56606c754d339aabaf0d14a6f5462d77f5f65acc047dc6036464fe4cc0af376c498950fe5107d11ad5e362ead0ab7d0a9f3e02b906354fa8c1b2461d1bc8b44245c7de4e9dbc0dde12fcdf9ec86f96b6df84e084dc0f51a1fa2578da9d4a0329b2a3bad44ba45288018fac4d4c877f87fb73da373da9c1e117c6431f1fe68c15c7ec08a426fcbe38b20e60ca56c4484a785863d7dfa825ef386612fa61b1d7bdbb97070e26f24f237fb5093ee23309607d0d607e24fec85875074e5a960f1fdc93fd0ee64cd06e03ea6a20fa02d3a08a8bee819d348b8e243c281b71b1a8ce0a27859ab201d21ec2a33d6fa299b567c34e7b97d2cfc4991f42d4fe17ab990f763639bf48028a4f87aca1bf5895f0508a50229834d9f3a04cd5d531eb2cdbca74569308bf09f1e5e13c4488de358c8fea8376a34',
        username: 'user5688806643',
        fio: 'lololol',
        token:
         'token',
        __v: 0 } },


Можете ли вы подсказать, как достать именно значение токена? Спасибо большое!
  • Вопрос задан
  • 741 просмотр
Решения вопроса 1
@StockholmSyndrome
судя по документации
Account.findOne({username: req.user}).select('token').exec() 
.then((token) => {
  // ...
});
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы