Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/eksap2.js Secret
Created Jul 2, 2018

Embed
What would you like to do?
example static and instance methods
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
name: String,
password: String
});
schema.statics.verifyPassword = async function(user, password) {
let u = await Test.findOne({ name: user });
return (u.password === password);
};
schema.methods.verifyPassword = function(someString) {
return (this.password === someString);
};
const Test = mongoose.model('test', schema);
const test = new Test({
name: 'Billy',
password: 'Th3K1d'
});
async function run() {
await conn.dropDatabase();
await test.save();
let gonnaFail = await Test.verifyPassword('Billy', 'myN@m31sH3nRy');
console.log(`first way: ${gonnaFail}`);
let billy = await Test.findOne();
console.log(`second way: ${billy.verifyPassword('Th3K1d')}`);
return conn.close();
}
run();
$: ./eksap2.js
first way: false
second way: true
$:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.