everything works fin until I add the friends to the user schema.
how do I get this to work? Im trying to allow users to have friends. Is there something small I am missing while trying to nest this document?
const mongoose = require('mongoose');
const bcrypt = require('bcrypt')
const UserSchema = new mongoose.Schema({
fullName: {
type: String,
},
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
image: {
type: String,
},
pictures: {
type: String,
},
nickName: {
type: String
},
relationshipStatus: {
type: String,
},
nextTrip: {
type: String,
},
reasonTravel: {
type: String,
},
language: {
type: String,
},
lastLocation: {
type: String,
},
loveLanguage: {
type: String,
},
travelGoals: {
type: String,
},
education: {
type: String,
},
friends: {
type: [UserSchema]
}
}, { timestamps: true });
UserSchema.pre('save', function(next){
const user = this;
if(!user.isModified('password')){
return next();
}
bcrypt.genSalt(10, (err,salt)=>{
if(err){
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash)=>{
if(err){
return next(err)
}
user.password = hash;
next();
})
})
})
UserSchema.methods.comparePassword = function(candidatePassword){
const user = this;
return new Promise ((resolve, reject)=>{
bcrypt.compare(candidatePassword, user.password, (err, isMatch)=>{
if(err){
return reject(err);
}
if(!isMatch){
return reject(false);
}
resolve(true);
})
})
}
const User = mongoose.model('User', UserSchema);
module.exports = User;
/Users/noahduran/Langiddy_msg/server/models/User.js:51 type: [UserSchema] ^
ReferenceError: Cannot access 'UserSchema' before initialization at Object. (/Users/noahduran/Langiddy_msg/server/models/User.js:51:16) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (/Users/noahduran/Langiddy_msg/server/config/jwt.config.js:2:14) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) [nodemon] app crashed - waiting for file changes before starting...
1条答案
按热度按时间vuktfyat1#
如果要在架构的某个属性中自引用该架构,请尝试使用
this
: