Mongoose Discriminators无法添加判别者详细信息

时间:2017-07-24 08:51:57

标签: node.js mongoose mongoose-schema

我正在努力为我的快速休息api添加鉴别器。我已使用鉴别器向用户架构添加了不同类型的用户,因为不同的用户需要其他信息。我面临的问题是,当我发布到api时,在添加信息时没有错误,只有一般信息被添加到模式中,标识符中的细节将被忽略。

架构如下:

var options = { discriminatorKey: 'type' };

var UserSchema = new Schema({
local: {
    email: {
        type: String,
        sparse: true,
        lowercase: true,
    },
    password: { type: String },
},
facebook: {
    id: String,
    token: String,
    email: String,
    name: String,
    profileIMG: String,
},
twitter: {
    id: String,
    token: String,
    displayName: String,
    username: String
},
google: {
    id: String,
    token: String,
    email: String,
    name: String,
    profileIMG: String,
}
}, options);

var addressSubschema = {
  street: {
    type: String,
    required: true
  },
  number: {
    type: String,
    required: true
  },
  city: {
    type: String,
    required: true
  },
};

var workingHoursSchema = {
  start: {
    type: String,
    required: true
  },
  finish: {
    type: String,
    required: true
  }
};

var adminSchema = new Schema({
  description: {
    type: String,
    required: true
  },
  category: {
    type: String,
    required: true
  },
  workingHours: workingHoursSchema,
  address: addressSubschema,
  workingRadius: {
    type: Number,
    required: true
  },
}, options);
var User = mongoose.model('User', UserSchema);
var Admin = User.discriminator('AdminUser', adminSchema);

module.exports = User;

然后我导出模型,在保存新用户时,我获得了成功,但管理员详细信息未保存。

User.findOne({'local.email': email}, function(err, existingUser) {

if (err) { return next(err) }
if (existingUser) {return res.status(422).json({error: "Email already exists"})}

var user = new User({
  "local.email": req.body.email,
  "local.password": req.body.password,
  "description": req.body.description,
  "category": req.body.category,
  "workingRadius": req.body.workingRadius,
  "street": req.body.street,
  "number": req.body.number,
  "city": req.body.city,
  "start": req.body.start,
  "finish": req.body.finish
});

user.save(function(err) {
  if (err) { return next(err) }
  res.json({success: true});
});

});

我很高兴使用鉴别器,所以非常感谢任何帮助。

0 个答案:

没有答案