Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

node.js - chekc fild is modified or not in mongoose and nodejs

I have this schema to create a user and in this schema I want check some property is modified or not:

I have a code like this:

import mongoose from 'mongoose';
import uniqueString from 'unique-string';
import { BaseSchema } from './BaseSchema';

enum Gender {
    Male = 1,
    Female = 2,
    Trans = 3
}

interface UserAttrs {
    firstName: string;
    lastName: string;
    phoneNumber: string;
    password: string;
}

export interface UserDoc extends mongoose.Document {
    firstName: string;
    lastName: string;
    phoneNumber: string;
    password: string;
}

interface UserModel extends mongoose.Model<UserDoc> {
    build(userAttrs: UserAttrs): UserDoc;
}

const UserSchema = new mongoose.Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    phoneNumber: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    }
})


UserSchema.pre('updateOne', function () {
    this.set({ securityStamp: uniqueString() });
});


UserSchema.statics.build = (attrs: UserAttrs) => {
    return new UserModel(attrs);
}


const UserModel = mongoose.model<UserDoc, UserModel>("User", UserSchema);

export { UserModel }

I want to check field is modified or not, in this way its not worked for me:

UserSchema.getUpdate().$set.password

but it show me this Error :

getUpdate is not function .

whats the problem ? how can i sovle this problem ?

question from:https://stackoverflow.com/questions/65950078/chekc-fild-is-modified-or-not-in-mongoose-and-nodejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...