У меня возникла проблема, связанная с UUID, при попытке создать экземпляр модели mongoose.
Мангуста Схема
export interface IRessourceModel extends IRessource, mongoose.Document { }
export let RessourceSchema: mongoose.Schema = new Schema({
uuid: {
type: String,
default: uuidv4() },
name: {
type: String,
required: true,
minlength: RessourceValidation.CONSTANTS.NAME_MIN_LENGTH,
maxlength: RessourceValidation.CONSTANTS.NAME_MAX_LENGTH,
trim: true,
validate: {
validator: RessourceValidation.name,
message: '{VALUE} is not a valid name'}},
type: {
type: String,
required: true,
enum: RessourceValidation.ENUM.TYPE,
validate: {
validator: RessourceValidation.type,
message: '{VALUE} is not a valid type'}},
uploaded: {
type: Date,
required: true,
default: Date.now }
})
export const Ressource: mongoose.Model<IRessourceModel> = mongoose.model<IRessourceModel>('Ressource', RessourceSchema);
* 1006 при создании экземпляра *
const RessourceModel = mongoose.model('Ressource');
// Some code here...
let Ressource: any = new RessourceModel({
name: req.body.name,
type: req.body.type,
})
Ressource.save((err, ressource) => {
if (err)
return (APIResponse.jsonErrorAfterMongooseQuery(res, 'Cannot create ressource', err));
APIResponse.json(res, ressource);
})
Выпуск
Когда я отправляю запрос POST на /ressources/
, который использует фрагмент кода выше, ресурс создается, но, если я отправляю другой запрос POST, второй созданный ресурс имеет тот же UUID, что и первый ...
Результаты для запроса POST # 1
{
"type": true,
"data": {
"uuid": "1794bbb4-3385-4b0d-909a-e22c60aee608",
"_id": "5b3a42f5ae71a02af4ed6d11",
"name": "Name of ressource 1",
"type": "IMAGE",
"uploaded": "2018-07-02T15:21:25.866Z",
"__v": 0
}
}
Результаты запроса POST # 2
{
"type": true,
"data": {
"uuid": "1794bbb4-3385-4b0d-909a-e22c60aee608",
"_id": "5b3a4530f3ab1f3d40b7ac93",
"name": "Name of ressource 2",
"type": "IMAGE",
"uploaded": "2018-07-02T15:30:56.379Z",
"__v": 0
}
}
Я неправильно использую default: uuidv4()
или это происходит от того, как я воплощаю модель мангуста? Или что-нибудь еще?
Я пытался установить UUID из функции Schema.pre('save')
, но безуспешно ...
Я немного растерялся, спасибо за вашу помощь!