fix: Mongoose 8 pre-save hook crash (next->async), v1.0.17 tag, test user

- user.schema.ts: convert pre-save from callback(next) to async - fixes
  'TypeError: next is not a function' on login (Mongoose 8 compat)
- Tag v1.0.17 for 1.0.18 build cycle
- scripts/seed-test-user.ts: utility to create test accounts
- docs: PROJECT-STATUS v4.9, AGENTS.md version bump
- Add test user test@yzrcloud.cn / 123456 (role: user)
This commit is contained in:
yuzhiran
2026-06-22 11:27:21 +08:00
parent 1e8e22c9ed
commit 4d54c8088c
4 changed files with 65 additions and 10 deletions
+55
View File
@@ -0,0 +1,55 @@
import { connect, disconnect } from 'mongoose'
import * as bcrypt from 'bcrypt'
import * as dotenv from 'dotenv'
import * as path from 'path'
dotenv.config({ path: path.resolve(__dirname, '../.env') })
async function main() {
const uri = process.env.MONGODB_URI
if (!uri) {
console.error('MONGODB_URI not set')
process.exit(1)
}
const conn = await connect(uri)
console.log('Connected to MongoDB')
const users = conn.connection.db!.collection('users')
const email = 'test@yzrcloud.cn'
const password = '123456'
const hashed = await bcrypt.hash(password, 10)
const existing = await users.findOne({ email })
if (existing) {
await users.updateOne(
{ email },
{ $set: { password: hashed, nickname: '测试用户', role: 'user', gravity: 5, interviewCredits: 1, remaining: 3 } }
)
console.log(`Updated test user: ${email}`)
} else {
await users.insertOne({
email,
password: hashed,
nickname: '测试用户',
role: 'user',
gravity: 5,
interviewCredits: 1,
remaining: 3,
interviewCount: 0,
plan: 'free',
createdAt: new Date(),
updatedAt: new Date(),
})
console.log(`Created test user: ${email}`)
}
await disconnect()
console.log('Done')
}
main().catch(err => {
console.error('Failed:', err)
process.exit(1)
})
+2 -3
View File
@@ -69,9 +69,8 @@ export class User {
export const UserSchema = SchemaFactory.createForClass(User)
UserSchema.pre('save', function (next) {
UserSchema.pre('save', async function () {
if (!this.phone && !this.wxOpenid && !this.email) {
return next(new Error('用户必须至少有一个联系方式(手机号/微信/邮箱)'))
throw new Error('用户必须至少有一个联系方式(手机号/微信/邮箱)')
}
next()
})