Questions tagged [mongoose-middleware]

26 questions
5
votes
1 answer

Mongoose middleware post update not working

Callback is not being called, but it should as per documented in mongoose middleware: schema.post('update', function(error, res, next) { if (error.name === 'MongoError' && error.code === 11000) { next(new Error('There was a duplicate key…
Tyro Hunter
  • 755
  • 1
  • 8
  • 20
4
votes
1 answer

Find a Document where value matched in either field using Mongoose Middleware

I have a list of account connections between source and target accounts so my schema looks like var ConnectionRequestSchema = new Schema({ sourceAccountId: { type: Schema.ObjectId, ref: 'Account' }, targetAccountId: { type:…
David Cruwys
  • 6,262
  • 12
  • 45
  • 91
3
votes
1 answer

Mongoose | Middleware | Rollback operations performed by pre/post hooks when an error is thrown

Considering the code below, how can a transaction be implemented in order to ensure that someParentDocument doesn't get deleted and any operations performed inside the hooks are rolledback, when an error is thrown in any of the hooks? const…
fmagno
  • 1,446
  • 12
  • 27
2
votes
2 answers

How to detect all changes,including deletion, made to a collection in MongoDB/Mongoose?

I have an app that uses MongoDB via mongoose. On the app there is a collection called Notifications. I would like to detect every time there is a change in that collection(including deleting a document) and take the appropriate action. I have read…
YulePale
  • 6,688
  • 16
  • 46
  • 95
2
votes
1 answer

Access mongoose parent document for default values in subdocument

I have a backend API for an Express/Mongo health tracking app. Each user has an array of weighIns, subdocuments that contain a value, a unit, and the date recorded. If no unit is specified the unit defaults to 'lb'. const WeighInSchema = new…
1
vote
0 answers

Cascade mongoose implement rollback when having errors

I am implement cascade like for mongodb using pre remove middleware of mongoose follow : Cascade style delete in Mongoose But I when having errors it not rollback deleted data. So how can we start transaction in pre middleware and pass it to handler…
HellCatVN
  • 866
  • 1
  • 6
  • 11
1
vote
2 answers

Mongoose Query Middleware Error(Query has already execute)

When I execute this model,I get error. Please solve this question. This problem brought me headache. Opposed to using 'Content here, content here', making it look like readable English, many desktop publishing packages and web page editors now use…
Grixx Nixx
  • 11
  • 3
1
vote
0 answers

using mongoose middleware with SchemaFactory

I am trying to use a mongoose middleware to recursively delete references to this object in another objects array (delete task references in taskList with the id from task) The problematic function: TaskSchema.pre('deleteOne', function(next) { …
Branchverse
  • 1,203
  • 1
  • 7
  • 19
1
vote
1 answer

How to register same mongoose hook for multiple methods?

I want to execute same script in the pre-hook with multiple methods like this: UserSchema.pre("findOne", function(next) { console.log("Common code"); }); & UserSchema.pre("findOneAndUpdate", function(next) { console.log("Common…
1
vote
0 answers

adding a pre middleware in the model to be extended in mongoose

i am using "mongoose-extend-schema": "^1.0.0" with mongoose to add a base schema to all other schemas. here is my code: import mongoose from "mongoose"; interface IBaseSchema extends mongoose.Document { createdBy: string; updatedBy: string; …
Shubham Shaw
  • 841
  • 1
  • 11
  • 24
1
vote
1 answer

How to silently skip saving a document when pre-save hook fails in Mongoose?

I'm adding a series of documents to the database. Each of these documents is validated in a pre-save hook. I want Mongoose to ignore saving that particular document that fails the validation without throwing any error so that the rest of the…
Saeed Ahadian
  • 212
  • 2
  • 4
  • 14
1
vote
2 answers

pre save middleware in mongoose

i am first time using pre save middleware and getting a bit confusion in it. It runs perfectly fine and also my save method is getting executed eventhough i am not calling the next() case 1 tourSchema.pre('save', function () { console.log('first…
rohit garg
  • 283
  • 3
  • 13
1
vote
0 answers

mongoose code only works if I make 2 http requests

Main objective: On call to route I either get a new or update existing CartItem object. The Object amount and total are passed in the middleware so the object total gets recalculated. The Schema seems to properly apply its middleware properly on the…
1
vote
1 answer

Mongoose pushing to an Array using presave middleware

I'm trying to update an array using presave hooks available in mongoose. Im using this array for Audit Purposes and hence i wanted to enforce correct values using these middle wares Here is my schema const taskSchema = new mongoose.Schema({ id: {…
josiebhai
  • 321
  • 3
  • 12
1
vote
0 answers

Not able to access schema properties in pre save middleware mongoose

Getting one weird issue, I'm not able to access any of my schema properties inside pre save middleware. When I mouse over on that property I'm able to see this error : "Property createdAt does not exist on type Document" following is my code…
Pankaj
  • 571
  • 5
  • 20
1
2