给定的是一个模型 UserModel
const UserSchema = mongoose.Schema(
{
username: {
type: String,
required: true,
trim: true, // trim preceding spaces and trailing whitespaces
index: { unique: true }, // the username should be unique
minlength: 6,
},
email: {
type: String,
required: true,
trim: true, // trim preceding spaces and trailing whitespaces
lowercase: true, // normalize email addresses to lowercase
index: { unique: true }, // the email address needs to be unique
},
password: {
type: String,
required: true,
minlength: 8, // A password needs to be at least 8 characters long
trim: true,
},
verified: {
type: Boolean,
default: false,
},
verificationToken: {
type: String,
required: true,
index: true,
unique: true,
default: () => crypto.randomBytes(20).toString('hex'),
},
resetTokens: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ResetToken' }],
},
{
timestamps: true,
}
);
module.exports = mongoose.model('User', UserSchema);
和密码重置令牌模型:
const PasswordResetTokenModel = mongoose.Schema(
{
_user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
index: true,
ref: 'User',
},
token: {
type: String,
required: true,
index: true,
unique: true,
default: () => crypto.randomBytes(20).toString('hex'),
},
},
{
timestamps: true,
}
);
module.exports = mongoose.model('ResetToken', PasswordResetTokenModel);
如果我现在打电话
UserModel.find()
.populate('resetTokens')
.sort({ createdAt: -1 })
.exec();
resetTokens
为空。
我以前做过,我知道这可能是微不足道的,但不知何故我无法弄清楚#是哪里出了问题。