Error in Mongoose findOne

0

I'm getting Error: Invalid argument to findOne(): 'texto pesquisado' when running findOne of Mongoose:

const User = mongoose.model('users', mySchema);
const query - 'texto pesquisado';

User.findOne(query, function(err, data) {
    if (err) return console.log('ERROR: ', err);
    return console.log('DATA: ', data);
});

I'm not finding the reason for this error because the function's arguments are apparently correct. Does anyone know what might be going on?

    
asked by anonymous 12.05.2017 / 23:41

1 answer

0

findOne expects a document / object as a parameter, not just a String as you passed. Check the example below:

const User = mongoose.model('users', mySchema);
const query = 'texto pesquisado';

User.findOne({nomecoluna: query}, function(err, data) {
    if (err) return console.log('ERROR: ', err);
    return console.log('DATA: ', data);
});

You can find more examples in the mongoose documentation mongoosejs.com

    
13.05.2017 / 00:32