text: const mongoose = require('mongoose');
  mongoose.set('useFindAndModify', false);
  const {isEmail} = require('validator');
  const bcrypt = require('bcryptjs');
  const userSchema = new mongoose.Schema({
      email: {
          type: String,
          required: [true, 'Įveskite el. paštą'],
          unique: true,
          lowercase : true,
          validate: [isEmail, 'Įveskite tinkamą el. paštą']
      },
      password: {
          type: String,
          required: [true, 'Įveskite slaptažodį'],
          minlength: [6, 'Minimalus simbolių skaičius – 6']
      },
      placeinfo: [
          {
          title: String,
          score: String
          }
      ]
  })
  
  // Before doc is saved
  userSchema.pre('save', async function (next) {
      const salt = await bcrypt.genSalt();
      this.password = await bcrypt.hash(this.password, salt);
      next();
  });
  // static method to login user
  userSchema.statics.login = async function (email, password) {
      const user = await this.findOne({email });
      if (user) {
        const auth = await  bcrypt.compare(password, user.password);
        if(auth) {
            return user;
        }
        throw Error('Neteisingas slaptazodis');
      }
      throw Error('Toks el. pastas neegzistuoja');
  }
  userSchema.statics.userPlace = async function (title) {
      const user1 = await this.findOne({'placeinfo.title': title});
      if (user1) {
            return user1;
      }
  
  }
  const User = mongoose.model('user', userSchema);
  
  module.exports = (User);