I'm currently working on a Node.js / Typescript / Mongoose webservice.
All my objects in my MongoDB have a property named "disable" of type Date.
By default, "disable" is set undefined. But when we delete an object, we pass to disable, the new Date() of deletion.
Since we don't have a real deletion, we need to send back a list by default that contains objects with "disable" as undefined. And also a list of objects who contain "disable" as NOT undefined.
I'm using Node/Typescript/Mongoose.
Do you have an idea how to make it?
Sorry for my poor English, I hope you can help me. Thank you very much!
UPDATE : I would like to use a filter on the request
My function to filter on undefined or defined "disable"
But the "!undefined" is sending an error
CodePudding user response:
What you could do is run a .find() on your model and then reduce the response to filter out objects that have the disable property set to undefined
let documents = await Model.find();
let disabledDocs = documents.reduce((all, obj) => {
return obj.disable !== undefined ? [...all, obj] : all;
}, []);
something like that.
CodePudding user response:
As I understand what you want is this:
....
const allDocs = await MyModel.find({})
const deletedDocs = allDocs.filter(doc => doc.disable === undefined)
const undeletedDocs = allDocs.filter(doc => doc.disable !== undefined)
.... some logic here
res.json({deletedDocs, undeletedDocs })
....
let me know in the comments if you accept the answer