Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require("mongoose"),
- bcrypt = require("bcrypt");
- mongoose.Promise = Promise; //!
- // userSchema = new mongoose.Schema({
- mongoose.connect(
- "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
- );
- mongoose.connection
- .once("open", () => {
- console.info("=================>mongoose connection open");
- })
- .on("error", error => {
- console.warn("!!!!!!!!!!!!!!!mongoose connection error", error);
- throw new Error(error);
- });
- const UserSchema = new mongoose.Schema({
- email: {
- type: String,
- unique: true,
- lowercase: true,
- required: [true, "Email is required."]
- },
- profile: {
- name: { type: String, default: "" },
- picture: { type: String, default: "" }
- },
- address: String,
- history: [
- {
- date: Date,
- paid: { type: Number, default: 0 },
- item: { type: mongoose.Schema.Types.ObjectId }
- }
- ]
- });
- UserSchema.pre("save", next => {
- const user = this;
- if (!user.isModified(`password`)) return next();
- bcrypt.genSalt(10, (error, salt) => {
- if (error) return next(error);
- bcrypt.hash(user.password, salt, null, (error, hash) => {
- //progress
- if (error) return next(error);
- user.password = hash;
- next();
- });
- });
- });
- UserSchema.methods.comparePassword = password =>
- bcrypt.compare(password, this.password);
- const User = mongoose.model("User", UserSchema);
- module.exports = mongoose.model(`User`, UserSchema);
Add Comment
Please, Sign In to add comment