这可能会在这里失败,但我很难绕过这个问题。
在我的Mongoose Schema中,我有一个群组列表。这些群体将是人,他们将拥有该人的_id,或者如果他们没有_id,他们将通过他们的电子邮件列出。
使用groups
对我来说是有道理的,所以我可以遍历列表而无需对列表名称进行硬编码,原因是我想让用户添加自己的组。我不理解的是......
1)我如何允许用户添加到此集合,我可以groups.push(groupName)
吗?不会猫鼬把它踢回来?另外,它如何知道我想在其他收集领域中使用ID,名称和电子邮件。
2)我需要显示组的名称,然后分别显示组中的个人。我不认为我能以这种方式展示钥匙。那么如何显示组的名称。
我认为我这样做基本上是错误的,但我无法找到正确的方法
const mongoose = require("mongoose"),
bcrypt = require('bcrypt-nodejs');
const passportLocalMongoose = require("passport-local-mongoose");
mongoose.createConnection("mongodb://localhost/familySite");
const userSchema = new mongoose.Schema({
username: {type:String, required: true, unique: true},
email: {type: String, required: false, unique: false},
password: { type: String, required: true},
resetPasswordToken: String,
resetPasswordExpires: Date,
profile: {picture: String, nickname: String},
permission:{app: Number}, //This will tell the app what level, 0 for Admin, 1 for User
name: {
first:String,
middle:String,
last:String,
other: {
startDate: Date,
endDate: Date,
first: String,
middle: String,
last: String
}
},
lastName: String,
address: String,
city: String,
state: String,
zipCode: Number,
phone: Number,
birthday: Date,
birthplace: String,
userCover: String,
categories: Array,
defaultPermission: Number,
events: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
moments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}],
instants: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Instant"
}],
groups:
[{family:{
type: mongoose.Schema.Types.ObjectId,
ref: "User"},
groupName: String,
name: String,
email: String
},
{friends:{
type: mongoose.Schema.Types.ObjectId,
ref: "User"},
name: String,
email: String,
groupName: String,
},
{colleagues:{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
name: String,
email: String,
groupName: String,
}
}],
likedMoments: [{ //This is for what this person likes, so they can refer later
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}],
likedEvents: [{ //This is for what this person likes, so they can refer later
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
favoriteEvents: [{ //This is for personal events that are favorited
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
favoriteMoments: [{ //This is for personal moments that are favorited
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}]
})
// I THINK I NEED TO MAKE THIS A METHOD SO I CAN GET IT ALL INTO APP.JS
// This uses bcrypt to hash passwords
userSchema.pre('save', function (next){
var user = this;
var SALT_FACTOR = 5;
if(!user.isModified('password')) return next();
bcrypt.genSalt(SALT_FACTOR, function (err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt,null, function (err, hash){
if(err) return next(err);
user.password - hash;
next();
});
});
});
userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", userSchema)