mongoose-sequence的AutoIncrementFactory没有生效
"mongoose-sequence": "^5.3.1"
已经按照老师的代码进行编写
打印了一下UserSchema已经有了id这个字段 但接口没有返回
import { Application } from "egg"
import { Schema } from "mongoose"
import * as AutoIncrementFactory from 'mongoose-sequence'
export interface UserProps {
username: string;
password: string;
email?: string;
nickname?: string;
picture?: string;
photoNumber?: string;
createdAt: Date;
updatedAt: Date;
}
function initUserModel(app: Application) {
const AutoIncrement = AutoIncrementFactory(app.mongoose)
const UserSchema = new Schema({
username: { type: String, unique: true, required: true },//unique 唯一性 required必须输入
password: { type: String, required: true },
email: { type: String },
nickname: { type: String },
picture: { type: String },
photoNumber: { type: String },
}, {
timestamps: true /*自动给更新时间和创建时间*/,
toJSON: {//定义方法 可以直接拿到返回对象使用
transform(_doc,ret) {
delete ret.password
delete ret.__v
}
}
})
UserSchema.plugin(AutoIncrement, { inc_field: 'id', id: 'users_id_counter' })
// return app.mongoose.model<UserProps>('User', UserSchema)
return app.mongoose.model<UserProps>('User', UserSchema);
}
export default initUserModel;
尝试过的解决方式:
1.删除node_modules和package-lock.json重新npm install
2.关闭vscode重启
正在回答 回答被采纳积分+1
这里视频里写法:
import * as AutoIncrementFactory from 'mongoose-sequence';
改成下面写法就能解决问题,这里好坑人
import AutoIncrementFactory from 'mongoose-sequence';
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星