How to check if that data already exist in the database during update (Mongoose And Express)

后端 未结 8 1920
灰色年华
灰色年华 2020-12-05 00:28

How to do validations before saving the edited data in mongoose?

For example, if sample.name already exists in the database, the user will receive a som

8条回答
  •  旧时难觅i
    2020-12-05 00:44

    For anybody falling on this old solution. There is a better way from the mongoose docs.

    var s = new Schema({ name: { type: String, unique: true }});
    s.path('name').index({ unique: true });
    

提交回复
热议问题