FindOneAndUpdate Не обновлять дискриминатор
Я работаю над REST API, используя Node, Express и Mongoose. Все работает отлично, когда я обновляю базовую модель. Но когда я пытаюсь обновить объект-дискриминатор sportEvent
, в этом случае он не работает.
Event.js. Модель данных событий имеет базовую схему, общую для всех коллекций с дискриминатором, для получения дополнительной информации для этой коллекции.
// base schema for all the events
// includes basic detail for all the events
const eventSchema = new Schema({
//title for the event
title: {
type: String,
required: true
},
//description for the events
description: {
type: String,
required: true
},
//event type for the event. such as Music, Sports, Expo, Leisure
eventType: {
type: String,
required: true,
}
}, { discriminatorKey: 'eventType' });
//sport event model for extending the basic event model
const sportEvent = Event.discriminator("sports", new Schema({
sportEvent: {
//sport name. for eg: cricket, football, etc
sportName: {
type: String,
required: true
},
//first team name
firstTeam: {
type: String,
required: true
},
//second team name
secondTeam: {
type: String,
required: true
},
}
}));
EventController.js - имеет метод PUT для обновления коллекции. Вот фрагмент кода.
//for updating the event added a PUT method in /event route
router.put('/events/:eventId', function(req, res, next){
//getting the event id form the url
eventId = req.params.eventId;
//checking the provided event id is a valid mongodb _id object or not
if(objectId.isValid(eventId)){
Event.findOneAndUpdate({_id: eventId}, {$set: req.body}, {new: true, runValidators: true}, function(err, event){
if(err) {
next(err);
}
sendResponse(res, "Event Successfully Updated", event);
});
} else {
//sending a bad request error to the user if the event id is not valid
sendError(res, 400, "Invalid Event ID");
}
});
Ответы
Ответ 1
Убедитесь, что ключ дискриминатора присутствует в объекте обновления или в качестве аргумента функции обновления, напишите случай переключателя на основе ключа дискриминатора, обновите запрос на конкретный тип схемы
callback = function(err, doc){
if(err) console.log(err)
console.log(doc)
};
var id = ObjectId("5a75d22e6dabf3102c059f56");
var update = {
title : 'title-name',
eventType : 'sports' ,
sportEvent : {
firstTeam : 'first-name',
secondTeam : 'second-name',
sportName : 'sport-name'
}
};
switch(update.eventType){
case 'sports':
SportEventSchema.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback)
break;
case 'games':
GameEventSchema.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback)
break;
default:
Event.findByIdAndUpdate(id, {$set : update}, {new : true, upsert : false}, callback);
break;
}
вывод: обновление для типа событий спорт
Mongoose: events.findAndModify({ eventType: 'sports', _id: ObjectId("5a75d22e6dabf3102c059f56") }, [], { '$set': { title: 'title-name', eventType: 'sports', sportEvent: { firstTeam: 'first-name', secondTeam: 'second-name', sportName: 'sport-name' } } }, { new: true, upsert: false, remove: false, fields: {} })
{ sportEvent:
{ firstTeam: 'first-name',
secondTeam: 'second-name',
sportName: 'sport-name' },
eventType: 'sports',
_id: 5a75d22e6dabf3102c059f56,
title: 'title-name',
description: 'desc',
__v: 0 }