Что такое «это» в пн goose проверить - PullRequest
0 голосов
/ 05 марта 2020

Попытка обновить поле с помощью findOneAndUpdate запускает код проверки.
Я предполагаю , это является понедельником goose .Query
Ошибка: ошибка findOne ()
НО Не удается вычислить из значения этого

это не экземпляр mon goose .Document
это не экземпляр mon goose .Query

// mongoose.Promise = require('bluebird');
// Validate email is not taken
UserSchema
    .path('email')
    .validate(function(value) {

console.log('THIS is >> ', this);
// OUTPUT :
// THIS is >>  Object [global] {
//   global: [Circular],
//   clearInterval: [Function: clearInterval],
//   clearTimeout: [Function: clearTimeout],
//   setInterval: [Function: setInterval],
//   setTimeout: [Function: setTimeout] { [Symbol(util.promisify.custom)]: [Function] },
//   queueMicrotask: [Function: queueMicrotask],
//   clearImmediate: [Function: clearImmediate],
//   setImmediate: [Function: setImmediate] {
//     [Symbol(util.promisify.custom)]: [Function]
//   }
// }

        return this.constructor.findOne({ email: value }).exec() // [ValidationError]: Validation failed: email: this.constructor.findOne is not a function
        return this.findOne({ email: value }).exec() // [ValidationError]: Validation failed: email: this.findOne is not a function
        return this.model.findOne({ email: value }).exec()  //  [ValidationError]: Validation failed: email: Cannot read property 'findOne' of undefined
            .then(user => {
                if(user) {
                    return false;
                }
console.log('USER NOT FOUND >>>>>>>');                     
                return true;
            })
            .catch(err => {
                throw err;
            });
    }, 'The specified email address is already in use.'); 


async function upadteUser(){
  const opts = { new: true, upsert: false, omitUndefined: true, runValidators: true};

   await User.findOneAndUpdate({ _id: userId} , {mobileno : '123456'}, opts).exec()
    .then(user => console.log(user))
    .catch(err);
}
...