These are chat archives for Automattic/mongoose

8th
Feb 2018
akilesh
@akileshv
Feb 08 2018 06:57
how to solve server localhost:27017 does not support collation
Kev
@lineus
Feb 08 2018 12:54
@akileshv what version of mongodb are you running? collation was added in 3.4 docs
Ben Chiciudean
@benydc
Feb 08 2018 16:29
I have another issue now, with my Scheme
userSchema.methods.comparePassword = comparePassword;

/**
 * Helper method for validating user's password.
 * @param {string} candidatePassword
 */
function comparePassword(candidatePassword: string) {
    return bcrypt.compare(`${candidatePassword}`, this.password)
        .catch((error) => {
            console.log("password failed", this.password);
            throw {err: 'Error validating password'};
        });
}
when I call the comparePassword, this.password is undefined
how I call it:
    const isMatch = await user.schema.methods.comparePassword(req.body.password);
    if (!isMatch) throw new UserAuthFailed();
Kev
@lineus
Feb 08 2018 16:32
the arrow function doesn't know about this
oh, wait. sorry i saw arrows and thought i knew a thing
does your console log have anything after "password failed"?
Ben Chiciudean
@benydc
Feb 08 2018 16:35
yes, undefined as I said
Kev
@lineus
Feb 08 2018 16:36
yeah, the arrow function prevents access to this in any context so that's expected.
Ben Chiciudean
@benydc
Feb 08 2018 16:36
so revert to callback instead of catch?
Kev
@lineus
Feb 08 2018 16:36
just change => to function ()
Ben Chiciudean
@benydc
Feb 08 2018 16:37
putting console.log(this.password) before bcrypt gives same result, undefined
so I don’t think that is the issue
Kev
@lineus
Feb 08 2018 16:40
i think calling the function on the schema directly will result in this.password being undefined because there is no document
you need to call that on an instance of the user model
Ben Chiciudean
@benydc
Feb 08 2018 16:42
hmmm
the instance of the user model doesn’t have my function
I think I need to define that in my interface…
Kev
@lineus
Feb 08 2018 16:49
#!/usr/bin/env node
'use strict'

const mongoose = require('./lib/spin')

const userSchema = mongoose.Schema({
  name: String,
  clearTextPassword: String
})

userSchema.methods.comparePassword = function (str) {
  if (this.clearTextPassword === str) {
    return true
  }
  return false
}

const User = mongoose.model('user', userSchema)

const u = new User({
  name: 'wobbles',
  clearTextPassword: 'daClown'
})

console.log(u.comparePassword('daClown')) // true
console.log(u.comparePassword('sldkfjsldkjf')) //false
fwiw, you have to add the compare method to the schema before you create the model with it. otherwise it won't be available to the instances of the model.
@benydc ^
Ben Chiciudean
@benydc
Feb 08 2018 16:56
I’m using .pre(’save’..)
pixiuPL
@pixiuPL
Feb 08 2018 16:58
I'd like to report a bug in Mongoose
where to do it?
Kev
@lineus
Feb 08 2018 16:58
@pixiuPL mongoose issue
pixiuPL
@pixiuPL
Feb 08 2018 16:59
@lineus Thank you
Kev
@lineus
Feb 08 2018 16:59
sure :)
pixiuPL
@pixiuPL
Feb 08 2018 16:59
Or you want me to fix it via PR as I may know where it lies
?
Kev
@lineus
Feb 08 2018 17:00
it's not me :) but they welcome pull requests. they have a guide for both issues and pull requests
pixiuPL
@pixiuPL
Feb 08 2018 17:01
Will do it. Thanks again :)
Kev
@lineus
Feb 08 2018 17:04
@benydc
#!/usr/bin/env node
'use strict'

const mongoose = require('./lib/spin')

const userSchema = mongoose.Schema({
  name: String,
  clearTextPassword: String
})

userSchema.methods.comparePassword = function (str) {
  if (this.clearTextPassword === str) {
    return true
  }
  return false
}

userSchema.pre('save', function (next) {
  console.log(this.comparePassword('daClown'))
  console.log(this.comparePassword('sldkfjsldkjf'))
  next()
})

const User = mongoose.model('user', userSchema)

const u = new User({
  name: 'wobbles',
  clearTextPassword: 'daClown'
})

u.save()
// true
// false
Ben Chiciudean
@benydc
Feb 08 2018 17:08
stil not working
when I console.log(this) I get the object but password is missing….
ah crap...
I was doing a select when I was finding the user…