Mongoose findone does not return values

0

Well, I have a problem here that I do not know what to try, I'm using express + mongoose.

My model

let mongoose = require('mongoose');
let users = mongoose.Schema({}, {strict: true});
module.exports = mongoose.model('users', users);

My controller

const usersModel = require('../models/users.js');

exports.token = async (req, res) => {
    let data = req.body;
    if (data.email === undefined || data.email === null) {
       return res.status(400).json({msg: 'E-mail é obrigatório'});
    }
    var user = usersModel.findOne({email: data.email});
    console.log(user);
}

Return from user

model {
'$__':
InternalCache {
strictMode: true,
selected: {},
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5bf1a5b535ff0f0038cea73c,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths:
    StateMachine {
    paths: { __v: 'init', _id: 'init' },
    states:
    { ignore: {},
        default: {},
        init: { __v: true, _id: true },
        modify: {},
        require: {} },
    stateNames: [ 'require', 'modify', 'init', 'default', 'ignore' ] },
pathsToScopes: {},
ownerDocument: undefined,
fullPath: undefined,
emitter:
    EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: 0 },
'$options': true },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
active: true,
password: 'sha1$0ff8d815$1$1ee620b675cd6f5f78f76c116023b9b00c5d79cc',
name: 'Nicolas',
email: '[email protected]',
_id: 5bf1a5b535ff0f0038cea73c },
'$init': true }

I do not know what I'm doing wrong, I wanted it to return only the values, can you help me?

Thank you:)

    
asked by anonymous 18.11.2018 / 20:11

2 answers

1

In the form you wrote it is as if you were associating the DocumentQuery with the variable. The correct way to get the result can be in two ways:

1 - Since you already used async in the function declaration, just put await before usersModel.findOne ({}).

2 - Declare a callback function, usersModel.findOne({},function(erro, user) {}) .

I hope I have helped.

    
21.11.2018 / 17:54
0

The await is missing.

If you gave async, you have to give await to wait for the query before displaying

Try this:

var user = await usersModel.findOne({email: data.email});
    
21.11.2018 / 17:38