Coder Social home page Coder Social logo

Comments (2)

HarishKalepu avatar HarishKalepu commented on May 23, 2024 1

Users Shema****************

import bcrypt from 'bcrypt-nodejs';
import mongoose from 'mongoose';
import autoref from 'mongoose-autorefs';
import autopopulate from 'mongoose-autopopulate';
var Schema = mongoose.Schema;

// Other oauthtypes to be added

/*
 User Schema
 */

const UserSchema = new mongoose.Schema({
  firstName: { type: String },
  lastName: { type: String },
  email: { type: String, unique: true, lowercase: true },
  password: String,
  mobileNumber: { type: String, unique: true, lowercase: true },
  dateOfBirth: { type: Date },
  profileImage: { type: String },
  role: { type: Number },
  organization: { type: Schema.Types.ObjectId, ref: 'Organizations', autopopulate:true },
  permissions: {
    fanClubs: { type: Boolean, enum: [true, false], default: false },
    fanCoins: { type: Boolean, enum: [true, false], default: false },
    funToWin: { type: Boolean, enum: [true, false], default: false },
    messageCenter: { type: Boolean, enum: [true, false], default: false },
    notifications: { type: Boolean, enum: [true, false], default: false }
  },
  manager: { type: Schema.Types.ObjectId, ref: 'Users', autopopulate:true },
  createdDate: { type: Date, default: Date.now },
  updatedDate: { type: Date, default: Date.now },
  createdBy: { type: Schema.Types.ObjectId, ref: 'Users', autopopulate:true },
  lastAccessTime: { type: Date, default: Date.now },
  isActive: { type: String, enum: ['Deactive', 'Active', 'Suspend'], default: ['Deactive'] },
  isEmailVerified: { type: Boolean, enum: [true, false], default: [false] },
  appId: { type: String, required: [true, 'Kindly enter your appId'] },
  appContext: { type: String, required: [true, 'Kindly enter your appContext'] },
  tokens: Array,
  resetPasswordToken: String,
  resetPasswordExpires: Date,
  google: {}
}, { versionKey: false, collection: "Users" });


UserSchema.plugin(autoref, [
  'organization.organizations',
  'createdBy.users'
])
UserSchema.plugin(autopopulate);

function encryptPassword(next) {
  const user = this;
  if (!user.isModified('password')) return next();
  return bcrypt.genSalt(5, (saltErr, salt) => {
    if (saltErr) return next(saltErr);
    return bcrypt.hash(user.password, salt, null, (hashErr, hash) => {
      if (hashErr) return next(hashErr);
      user.password = hash;
      return next();
    });
  });
}

/**
 * Password hash middleware.
 */
UserSchema.pre('save', encryptPassword);

/*
 Defining our own custom document instance method
 */
UserSchema.methods = {
  comparePassword(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
      if (err) return cb(err);
      return cb(null, isMatch);
    });
  }
};

/**
 * Statics
 */

UserSchema.statics = {};

export default mongoose.model('Users', UserSchema);

Organization Schema*

/**
 * Schema Definitions
 */
import mongoose from 'mongoose';
import autoref from 'mongoose-autorefs';
import autopopulate from 'mongoose-autopopulate';
var Schema = mongoose.Schema;

var OrganizationsSchema = new Schema({
  firstName: { type: String, required: [true, 'Kindly enter your firstName'] },
  lastName: { type: String, required: [true, 'Kindly enter your lastName'] },
  organizationName: { type: String, required: [true, 'Kindly enter your Org Name'], unique: true, lowercase: true },
  typeOfBusiness: { type: String, required: [true, 'Kindly enter Type Of Business'] },
  email: { type: String, required: [true, 'Kindly enter your email'], unique: true, lowercase: true },
  mobileNumber: { type: String, required: [true, 'Kindly enter your mobile number'], unique: true, lowercase: true },
  password: { type: String, required: [true, 'Kindly enter your password'] },
  createdDate: { type: Date, default: Date.now },
  updatedDate: { type: Date, default: Date.now },
  appId: { type: String, required: [true, 'Kindly enter your password'] },
  appContext: { type: String, required: [true, 'Kindly enter your password'] },
  users: [{ type: Schema.Types.ObjectId, ref: 'Users', ref: 'Users', autopopulate: true }],
  plan: { type: Schema.Types.ObjectId, ref: 'Plans', autopopulate: true }
}, { versionKey: false, collection: "Organizations" });

// Compiles the schema into a model, opening (or creating, if
// nonexistent) the 'Topic' collection in the MongoDB database
OrganizationsSchema.plugin(autoref, [
  'plan.plans',
  'users.users'
])
OrganizationsSchema.plugin(autopopulate);
export default mongoose.model('Organizations', OrganizationsSchema);

Note : if i am hitting getting service i am getting response if i am hitting post service data saving but response not coming

Please suggest me solution for this one ASAP please

from mongoose-autopopulate.

vkarpov15 avatar vkarpov15 commented on May 23, 2024

Can you please provide more information re: ☝️ ? What is the actual issue you're seeing?

from mongoose-autopopulate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.