在 mongoose 節點 js 中使用擴展模式時丟失資料我需要創建一個用戶模型和客戶端從用戶擴展。我正在安裝“mongoose-extend-schema”包我的模型:
const userSchema = new mongoose.Schema({
nom: String,
prenom: String,
email: String,
password: String,
confirm_password: String,
matricule: String,
fonction: String,
role: {
type: String,
default: "anonyme",
},
activatedMail: { type: Boolean, default: false },
activationToken: { type: String, default: "" },
emailTokenExpires:{type:Date, default: null },
resetPasswordToken: { type: String, default: "" },
resetPasswordExpires:{type:Date,default: null},
Complement_adresse: String,
ville: String,
pays: String,
cp: String,
tel: String,
rue: String,
createdOn: { type: Date, default: Date.now },
});
const User = mongoose.model("User", userSchema);
//Client extends from User model
const clientSchema = extendSchema(userSchema, {
rep:{
type: String,
default:"EA000"
},
Src_Client: {
type: String,
enum: ["prospect","externe"],
default:"externe"
},
Date_src: {
type: String,
},
Date_StatQ: {
type: String,
},
Date_StatCC: {
type: String,
},
Date_StatCp: {
type: String,
},
Date_StatCs: {
type: String,
},
Date_StatEs: {
type: String,
},
descriptionClient: {
type: String,
}
});
exports.userSchema = userSchema;
exports.User = User;
當我從用戶模式創建一個新的客戶端擴展時,一些資料丟失,回應僅從用戶獲取資料并忽略來自客戶端的資料
client = new User({
matricule: matClient,
nom: req.body.nom,
role:"Client",
prenom: req.body.prenom,
fonction: req.body.fonction,
descriptionClient:req.body.descriptionClient,
email: req.body.email,
tel: req.body.tel,
rue: req.body.rue,
ville: req.body.ville,
pays: req.body.pays,
type: req.body.type,
email2: req.body.email2,
Complement_adresse: req.body.Complement_adresse,
cp: req.body.cp,
tel2: req.body.tel2,
Src_Client: "prospect",
Date_src: req.body.Date_src,
Date_StatQ: req.body.Date_StatQ,
Date_StatCC: req.body.Date_StatCC,
Date_StatCp: req.body.Date_StatCp,
Date_StatCs: req.body.Date_StatCs,
Date_StatEs: req.body.Date_StatEs
});
這是郵遞員的一個例子

uj5u.com熱心網友回復:
只需在用戶模型的客戶端模型中添加相同的屬性:
const userSchema = new mongoose.Schema({
nom: String,
prenom: String,
email: String,
password: String,
//other fields...
Add properties of client here
rep:{
type: String,
default:"EA000"
},
Src_Client: {
type: String,
enum: ["prospect","externe"],
default:"externe"
},
Date_src: {
type: String,
},
Date_StatQ: {
type: String,
},
Date_StatCC: {
type: String,
},
Date_StatCp: {
type: String,
},
Date_StatCs: {
type: String,
},
Date_StatEs: {
type: String,
},
descriptionClient: {
type: String,
},
});
您可以查看此鏈接以獲取更多詳細資訊:https : //www.npmjs.com/package/mongoose-extend-schema
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/355644.html
