Nodejs ile firavun faresi kullanarak cevap arayanlar için. Ben böyle yaparım.
exports.deletePregunta = function (req, res) {
let codTest = req.params.tCodigo;
let indexPregunta = req.body.pregunta;
let inPregunta = `tPreguntas.0.pregunta.${indexPregunta}`;
let inOpciones = `tPreguntas.0.opciones.${indexPregunta}`;
let inTipo = `tPreguntas.0.tipo.${indexPregunta}`;
Test.findOneAndUpdate({ tCodigo: codTest },
{
'$unset': {
[inPregunta]: 1,
[inOpciones]: 1,
[inTipo]: 1
}
}).then(()=>{
Test.findOneAndUpdate({ tCodigo: codTest }, {
'$pull': {
'tPreguntas.0.pregunta': null,
'tPreguntas.0.opciones': null,
'tPreguntas.0.tipo': null
}
}).then(testModificado => {
if (!testModificado) {
res.status(404).send({ accion: 'deletePregunta', message: 'No se ha podido borrar esa pregunta ' });
} else {
res.status(200).send({ accion: 'deletePregunta', message: 'Pregunta borrada correctamente' });
}
})}).catch(err => { res.status(500).send({ accion: 'deletePregunta', message: 'error en la base de datos ' + err }); });
}
Çok iyi anlamazsa bu cevabı yeniden yazabilirim, ama bence sorun değil.
Umarım bu size yardımcı olur, bu sorunla yüzleşmek için çok zaman kaybettim.